Skip to content

Commit

Permalink
first version of perl test
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Dec 1, 2024
1 parent 4265e0e commit 7698c78
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
66 changes: 65 additions & 1 deletion src/package_test/run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/recipe/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

Expand Down
24 changes: 24 additions & 0 deletions src/recipe/parser/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

/// A test that runs the tests of a downstream package.
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct DownstreamTest {
Expand All @@ -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
Expand Down Expand Up @@ -383,6 +395,18 @@ impl TryConvertNode<CommandsTest> for RenderedMappingNode {
}
}

///////////////////////////
/// Perl Test ///
///////////////////////////

impl TryConvertNode<PerlTest> for RenderedMappingNode {
fn try_convert(&self, _name: &str) -> Result<PerlTest, Vec<PartialParsingError>> {
let mut perl_test = PerlTest::default();
validate_keys!(perl_test, self.iter(), uses);
Ok(perl_test)
}
}

///////////////////////////
/// Package Contents ///
///////////////////////////
Expand Down

0 comments on commit 7698c78

Please sign in to comment.