From 7698c78f5ce686ca2fa0cddce4e5307defad36d7 Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Sun, 1 Dec 2024 09:29:33 +0100 Subject: [PATCH] first version of `perl` test --- src/package_test/run_test.rs | 66 +++++++++++++++++++++++++++++++++++- src/recipe/parser.rs | 2 +- src/recipe/parser/test.rs | 24 +++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/src/package_test/run_test.rs b/src/package_test/run_test.rs index 7dc54634..6e5f311e 100644 --- a/src/package_test/run_test.rs +++ b/src/package_test/run_test.rs @@ -33,7 +33,8 @@ use crate::{ env_vars, metadata::PlatformWithVirtualPackages, recipe::parser::{ - CommandsTest, DownstreamTest, PythonTest, PythonVersion, Script, ScriptContent, TestType, + CommandsTest, DownstreamTest, PerlTest, PythonTest, PythonVersion, Script, ScriptContent, + TestType, }, render::solver::create_environment, source::copy_dir::CopyDir, @@ -425,6 +426,10 @@ pub async fn run_test( .run_test(&pkg, &package_folder, &prefix, &config) .await? } + TestType::Perl { perl } => { + perl.run_test(&pkg, &package_folder, &prefix, &config) + .await? + } TestType::Downstream(downstream) if downstream_package.is_none() => { downstream .run_test(&pkg, package_file, &prefix, &config) @@ -591,6 +596,65 @@ impl PythonTest { } } +impl PerlTest { + /// Execute the Perl test + pub async fn run_test( + &self, + pkg: &ArchiveIdentifier, + path: &Path, + prefix: &Path, + config: &TestConfiguration, + ) -> Result<(), TestError> { + let span = tracing::info_span!("Running perl test"); + let _guard = span.enter(); + + let match_spec = MatchSpec::from_str( + format!("{}={}={}", pkg.name, pkg.version, pkg.build_string).as_str(), + ParseStrictness::Lenient, + )?; + + let dependencies = vec![ + MatchSpec::from_str("perl", ParseStrictness::Strict).unwrap(), + match_spec, + ]; + + create_environment( + "test", + &dependencies, + config + .host_platform + .as_ref() + .unwrap_or(&config.current_platform), + prefix, + &config.channels, + &config.tool_configuration, + config.channel_priority, + config.solve_strategy, + ) + .await + .map_err(TestError::TestEnvironmentSetup)?; + + let mut imports = String::new(); + for module in &self.uses { + writeln!(imports, "use {}", module)?; + } + + let script = Script { + content: ScriptContent::Command(imports.clone()), + interpreter: Some("perl".into()), + ..Script::default() + }; + + let tmp_dir = tempfile::tempdir()?; + script + .run_script(Default::default(), tmp_dir.path(), path, prefix, None, None) + .await + .map_err(|e| TestError::TestFailed(e.to_string()))?; + + Ok(()) + } +} + impl CommandsTest { /// Execute the command test pub async fn run_test( diff --git a/src/recipe/parser.rs b/src/recipe/parser.rs index c740b1bc..f1ab2501 100644 --- a/src/recipe/parser.rs +++ b/src/recipe/parser.rs @@ -49,7 +49,7 @@ pub use self::{ source::{GitRev, GitSource, GitUrl, PathSource, Source, UrlSource}, test::{ CommandsTest, CommandsTestFiles, CommandsTestRequirements, DownstreamTest, - PackageContentsTest, PythonTest, PythonVersion, TestType, + PackageContentsTest, PerlTest, PythonTest, PythonVersion, TestType, }, }; diff --git a/src/recipe/parser/test.rs b/src/recipe/parser/test.rs index 4f940cee..1f0ac010 100644 --- a/src/recipe/parser/test.rs +++ b/src/recipe/parser/test.rs @@ -121,6 +121,13 @@ impl Default for PythonTest { } } +/// A special Perl test that checks if the imports are available and runs `cpanm check`. +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct PerlTest { + /// List of perl `uses` to test + pub uses: Vec, +} + /// A test that runs the tests of a downstream package. #[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] pub struct DownstreamTest { @@ -137,6 +144,11 @@ pub enum TestType { /// The imports to test and the `pip check` flag python: PythonTest, }, + /// A Perl test that will test if the modules are available + Perl { + /// The modules to test + perl: PerlTest + }, /// A test that executes multiple commands in a freshly created environment Command(CommandsTest), /// A test that runs the tests of a downstream package @@ -383,6 +395,18 @@ impl TryConvertNode for RenderedMappingNode { } } +/////////////////////////// +/// Perl Test /// +/////////////////////////// + +impl TryConvertNode for RenderedMappingNode { + fn try_convert(&self, _name: &str) -> Result> { + let mut perl_test = PerlTest::default(); + validate_keys!(perl_test, self.iter(), uses); + Ok(perl_test) + } +} + /////////////////////////// /// Package Contents /// ///////////////////////////