Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard against garbage SPDX #3

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Run Tests
on: [ pull_request ]

concurrency:
group: tests-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
tests:
strategy:
fail-fast: false
runs-on: ubuntu-22.04
name: Tests
steps:
- uses: actions-rust-lang/setup-rust-toolchain@v1

- uses: actions/checkout@v4

- name: Run Tests
run: |
cargo test
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ krates = "0.17.5"
spdx = "0.10.8"
reqwest = { version = "0.12.12", default-features = false, features = ["blocking", "rustls-tls"] }
serde = { version = "1.0.217", features = ["derive"] }

[dev-dependencies]
serde_json = "1.0.140"
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,19 @@ impl Serialize for Expression {
}
}

fn deserialize_expression<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<Expression>, D::Error> {
match Option::<Expression>::deserialize(deserializer) {
Err(_) => Ok(None),
Ok(opt) => Ok(opt),
}
}

#[derive(Serialize, Deserialize)]
pub struct LicenseFile {
/// Filename of the license file
pub name: String,
/// If known, the SPDX identifier of the license
#[serde(default, deserialize_with = "deserialize_expression")]
pub spdx: Option<Expression>,
/// The content of the license file
pub text: String,
Expand All @@ -116,6 +124,7 @@ pub struct Package {
/// Url of the package (this might be the repository or the crates.io page or a homepage)
pub package_url: Option<String>,
/// If known, the combined SPDX expression for all licenses of the package (e.g. MIT OR Apache-2.0)
#[serde(default, deserialize_with = "deserialize_expression")]
pub license_spdx: Option<Expression>,
/// All the license files that couldd be found for the package
pub license_files: Vec<LicenseFile>,
Expand Down Expand Up @@ -390,3 +399,29 @@ fn licenses_in_expr(expr: &spdx::Expression) -> usize {
fn licenses_in_expr_opt(expr: Option<&Expression>) -> usize {
expr.map(|expr| licenses_in_expr(&expr.0)).unwrap_or(0)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_garbage_spdx() {
let json = r#"{ "name": "test", "spdx": "garbage", "text": "AAA" }"#;
let x: LicenseFile = serde_json::from_str(json).unwrap();
assert!(x.spdx.is_none());
}

#[test]
fn test_non_garbage_spdx() {
let json = r#"{ "name": "test", "spdx": "MIT", "text": "AAA" }"#;
let x: LicenseFile = serde_json::from_str(json).unwrap();
assert_eq!(format!("{}", x.spdx.unwrap()), "MIT");
}

#[test]
fn missing_spdx() {
let json = r#"{ "name": "test", "text": "AAA" }"#;
let x: LicenseFile = serde_json::from_str(json).unwrap();
assert!(x.spdx.is_none());
}
}