Skip to content

Commit

Permalink
alpkit: Extract invalid fixtures to funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed Aug 28, 2023
1 parent c479f86 commit 0e07c28
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
32 changes: 17 additions & 15 deletions alpkit/src/apkbuild.test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde_json::json;
use super::*;
use crate::internal::test_utils::{assert, assert_from_to_json, assert_let, S};

fn sample_apkbuild() -> Apkbuild {
fn valid_apkbuild() -> Apkbuild {
Apkbuild {
maintainer: Some(S!("Jakub Jirutka <jakub@jirutka.cz>")),
contributors: vec![
Expand Down Expand Up @@ -67,16 +67,8 @@ fn sample_apkbuild() -> Apkbuild {
}
}

#[test]
#[cfg(feature = "validate")]
fn apkbuild_validate_valid() {
assert!(sample_apkbuild().validate(&()).is_ok())
}

#[test]
#[cfg(feature = "validate")]
fn apkbuild_validate_invalid() {
let apkbuild = Apkbuild {
fn invalid_apkbuild() -> Apkbuild {
Apkbuild {
maintainer: Some(S!("Invalid em@il")),
contributors: vec![
S!("invalid@form.at"),
Expand Down Expand Up @@ -122,16 +114,26 @@ fn apkbuild_validate_invalid() {
Secfix::new("1.2.0-ra", vec![S!("CVE-2021-12345")]),
],
..Default::default()
};
}
}

assert_let!(Err(e) = apkbuild.validate(&()));
#[test]
#[cfg(feature = "validate")]
fn apkbuild_validate_valid() {
assert!(valid_apkbuild().validate(&()).is_ok())
}

#[test]
#[cfg(feature = "validate")]
fn apkbuild_validate_invalid() {
assert_let!(Err(e) = invalid_apkbuild().validate(&()));
assert!(e.flatten().len() == 24);
}

#[test]
fn read_apkbuild() {
let fixture = Path::new("../fixtures/aports/sample/APKBUILD");
assert!(ApkbuildReader::new().read_apkbuild(fixture).unwrap() == sample_apkbuild());
assert!(ApkbuildReader::new().read_apkbuild(fixture).unwrap() == valid_apkbuild());
}

#[test]
Expand Down Expand Up @@ -244,7 +246,7 @@ fn test_decode_source_and_sha512sums() {
#[test]
fn apkbuild_json() {
assert_from_to_json!(
sample_apkbuild(),
valid_apkbuild(),
json!({
"maintainer": "Jakub Jirutka <jakub@jirutka.cz>",
"contributors": [
Expand Down
56 changes: 29 additions & 27 deletions alpkit/src/package/pkginfo.test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::internal::test_utils::{assert, assert_from_to_json, assert_let, S};

use super::*;

fn sample_pkginfo() -> PkgInfo {
fn valid_pkginfo() -> PkgInfo {
PkgInfo {
pkgname: S!("sample"),
pkgver: S!("1.2.3-r2"),
Expand All @@ -31,6 +31,29 @@ fn sample_pkginfo() -> PkgInfo {
}
}

fn invalid_pkginfo() -> PkgInfo {
PkgInfo {
pkgname: S!("samp/le"),
pkgver: S!("1-2_3-r2"),
pkgdesc: S!("A sample aport\nfor testing"),
url: S!("ftp://example.org/sample"),
arch: S!("AArch64"),
origin: S!("sampl*"),
commit: Some(S!("123")),
maintainer: Some(S!("Not an em@il")),
license: S!("multi\nline"),
triggers: vec![S!("bin/*"), S!("/usr bin/*")],
depends: Dependencies::parse(["ruby#>=3.0", "so:libc.musl-x86_64.$o.1"]).unwrap(),
conflicts: Dependencies::parse(["sample-legacy!"]).unwrap(),
install_if: Dependencies::parse(["sample=1.2.3-alpha", "-"]).unwrap(),
provides: Dependencies::parse(["cmd:sample*=1.2.3-r2"]).unwrap(),
provider_priority: Some(10),
packager: S!("Not an em@il"),
datahash: S!("123"),
..Default::default()
}
}

#[test]
fn pkginfo_parse() {
let input = indoc! {"
Expand Down Expand Up @@ -59,40 +82,19 @@ fn pkginfo_parse() {
depend = so:libc.musl-x86_64.so.1
datahash = 4c36284c04dd1e18e4df59b4bc873fd89b6240861b925cac59341cc66e36d94b
"};
assert!(PkgInfo::parse(input).unwrap() == sample_pkginfo());
assert!(PkgInfo::parse(input).unwrap() == valid_pkginfo());
}

#[test]
#[cfg(feature = "validate")]
fn pkginfo_validate_valid() {
assert!(sample_pkginfo().validate(&()).is_ok());
assert!(valid_pkginfo().validate(&()).is_ok());
}

#[test]
#[cfg(feature = "validate")]
fn pkginfo_validate_invalid() {
let pkginfo = PkgInfo {
pkgname: S!("samp/le"),
pkgver: S!("1-2_3-r2"),
pkgdesc: S!("A sample aport\nfor testing"),
url: S!("ftp://example.org/sample"),
arch: S!("AArch64"),
origin: S!("sampl*"),
commit: Some(S!("123")),
maintainer: Some(S!("Not an em@il")),
license: S!("multi\nline"),
triggers: vec![S!("bin/*"), S!("/usr bin/*")],
depends: Dependencies::parse(["ruby#>=3.0", "so:libc.musl-x86_64.$o.1"]).unwrap(),
conflicts: Dependencies::parse(["sample-legacy!"]).unwrap(),
install_if: Dependencies::parse(["sample=1.2.3-alpha", "-"]).unwrap(),
provides: Dependencies::parse(["cmd:sample*=1.2.3-r2"]).unwrap(),
provider_priority: Some(10),
packager: S!("Not an em@il"),
datahash: S!("123"),
..Default::default()
};

assert_let!(Err(e) = pkginfo.validate(&()));
assert_let!(Err(e) = invalid_pkginfo().validate(&()));
assert!(e.flatten().len() == 18);
}

Expand All @@ -118,7 +120,7 @@ fn parse_key_value_with_missing_equals() {
#[test]
fn pkginfo_json() {
assert_from_to_json!(
sample_pkginfo(),
valid_pkginfo(),
json!({
"maintainer": "Jakub Jirutka <jakub@jirutka.cz>",
"pkgname": "sample",
Expand Down Expand Up @@ -198,6 +200,6 @@ fn pkginfo_json_with_dependency_arrays() {

assert_json_eq!(
serde_json::from_str::<PkgInfo>(&pkginfo_json).unwrap(),
sample_pkginfo()
valid_pkginfo()
);
}

0 comments on commit 0e07c28

Please sign in to comment.