embed-licensing 0.3.1

Embed licensing information of dependencies to comply with free software licenses
Documentation
// SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
//
// SPDX-License-Identifier: MPL-2.0

//! See the comments in embed_licensing_core::collect_tree on the motivation for this test.

use std::rc::Rc;

use pretty_assertions::assert_eq;

use embed_licensing::{collect_from_manifest, CollectConfig, Crate, CrateLicense, Licensing};

mod common;
use common::*;

#[test]
fn cycle() -> Result<()> {
    let pkg2 = Rc::new(TestPackage::new(SimpleManifest {
        name: "pkg2".to_string(),
        version: "2.0.0".to_string(),
        license: Some(spdx::Expression::parse("BSD-3-Clause")?),
        license_file: None,
        authors: vec!["Jack Person <jane.person@example.com>".to_string()],
        repository: Some("https://example.com/pkg2.git".to_string()),
        dependencies: vec![].into(),
        dev_dependencies: vec![].into(),
        build_dependencies: vec![].into(),
    })?);
    let pkg1 = Rc::new(TestPackage::new(SimpleManifest {
        name: "pkg1".to_string(),
        version: "1.0.0".to_string(),
        license: Some(spdx::Expression::parse("Apache-2.0 OR MIT")?),
        license_file: None,
        authors: vec!["Jane Person <jane.person@example.com>".to_string()],
        repository: Some("https://example.com/pkg1.git".to_string()),
        dependencies: vec![(None, pkg2.clone())].into(),
        dev_dependencies: vec![].into(),
        build_dependencies: vec![].into(),
    })?);

    pkg2.manifest
        .dev_dependencies
        .borrow_mut()
        .push((None, pkg1.clone()));
    pkg2.update_files()?;

    // In this simple case, the same output is expected,
    // no matter which package is used as root.
    let expected = Licensing {
        packages: vec![
            Crate {
                name: "pkg1".to_string(),
                version: "1.0.0".to_string(),
                authors: vec!["Jane Person <jane.person@example.com>".to_string()],
                license: CrateLicense::SpdxExpression(spdx::Expression::parse(
                    "Apache-2.0 OR MIT",
                )?),
                website: "https://example.com/pkg1.git".to_string(),
            },
            Crate {
                name: "pkg2".to_string(),
                version: "2.0.0".to_string(),
                authors: vec!["Jack Person <jane.person@example.com>".to_string()],
                license: CrateLicense::SpdxExpression(spdx::Expression::parse("BSD-3-Clause")?),
                website: "https://example.com/pkg2.git".to_string(),
            },
        ],
        licenses: vec!["Apache-2.0", "BSD-3-Clause", "MIT"]
            .into_iter()
            .map(spdx::license_id)
            .map(Option::unwrap)
            .collect(),
        exceptions: vec![],
    };

    assert_eq!(
        collect_from_manifest(
            pkg1.manifest_path(),
            CollectConfig {
                dev: true,
                build: false,
                ..Default::default()
            }
        )?,
        expected
    );

    assert_eq!(
        collect_from_manifest(
            pkg2.manifest_path(),
            CollectConfig {
                dev: true,
                build: false,
                ..Default::default()
            }
        )?,
        expected
    );

    Ok(())
}