From 5088cac5f51fd73b0b663a991fcbdce41278437a Mon Sep 17 00:00:00 2001 From: nokazn <41154684+nokazn@users.noreply.github.com> Date: Sat, 10 Feb 2024 15:29:46 +0900 Subject: [PATCH] test: add some tests for lockfile --- src/lockfile.rs | 96 +++++++++++++++++- src/utils/glob.rs | 4 +- src/utils/tests.rs | 14 +++ tests/fixtures/lockfile/bun/bun.lockb | Bin 0 -> 1322 bytes tests/fixtures/lockfile/bun/package.json | 5 + tests/fixtures/lockfile/nope/.gitkeep | 0 tests/fixtures/lockfile/npm/package-lock.json | 24 +++++ tests/fixtures/lockfile/npm/package.json | 5 + tests/fixtures/lockfile/pnpm/package.json | 5 + tests/fixtures/lockfile/pnpm/pnpm-lock.yaml | 18 ++++ tests/fixtures/lockfile/yarn/package.json | 5 + tests/fixtures/lockfile/yarn/yarn.lock | 8 ++ 12 files changed, 181 insertions(+), 3 deletions(-) create mode 100755 tests/fixtures/lockfile/bun/bun.lockb create mode 100644 tests/fixtures/lockfile/bun/package.json create mode 100644 tests/fixtures/lockfile/nope/.gitkeep create mode 100644 tests/fixtures/lockfile/npm/package-lock.json create mode 100644 tests/fixtures/lockfile/npm/package.json create mode 100644 tests/fixtures/lockfile/pnpm/package.json create mode 100644 tests/fixtures/lockfile/pnpm/pnpm-lock.yaml create mode 100644 tests/fixtures/lockfile/yarn/package.json create mode 100644 tests/fixtures/lockfile/yarn/yarn.lock diff --git a/src/lockfile.rs b/src/lockfile.rs index 6297bb4..19571b8 100644 --- a/src/lockfile.rs +++ b/src/lockfile.rs @@ -8,7 +8,7 @@ use strum::IntoEnumIterator; use crate::{core::PackageManagerKind, errors::Error}; -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct Lockfile { pub kind: PackageManagerKind, path: PathBuf, @@ -43,3 +43,97 @@ impl Lockfile { Ok(hash) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::{core::PackageManagerKind, test_each, utils::path::to_absolute_path}; + + struct NewTestCase { + input: &'static str, + expected: (PackageManagerKind, PathBuf), + } + + fn test_new_each(case: NewTestCase) { + let lockfile = Lockfile::new(case.input).unwrap(); + assert_eq!(lockfile.kind, case.expected.0); + assert_eq!(lockfile.path, case.expected.1); + } + + test_each!( + test_new, + "npm" => NewTestCase { + input: "./tests/fixtures/lockfile/npm", + expected: ( + PackageManagerKind::Npm, + PathBuf::from("./tests/fixtures/lockfile/npm/package-lock.json") + ), + }, + "yarn" => NewTestCase { + input: "./tests/fixtures/lockfile/yarn", + expected: ( + PackageManagerKind::Yarn, + PathBuf::from("./tests/fixtures/lockfile/yarn/yarn.lock") + ), + }, + "pnpm" => NewTestCase { + input: "./tests/fixtures/lockfile/pnpm", + expected: ( + PackageManagerKind::Pnpm, + PathBuf::from("./tests/fixtures/lockfile/pnpm/pnpm-lock.yaml") + ), + }, + "bun" => NewTestCase { + input: "./tests/fixtures/lockfile/bun", + expected: ( + PackageManagerKind::Bun, + PathBuf::from("./tests/fixtures/lockfile/bun/bun.lockb") + ), + }, + ); + + #[test] + fn test_new_nope() { + let lockfile = Lockfile::new("tests/fixtures/lockfile/nope"); + assert_eq!( + lockfile.unwrap_err().to_string(), + format!( + "No lockfile at: `{}`", + to_absolute_path("tests/fixtures/lockfile/nope") + .unwrap() + .to_string_lossy() + ) + ); + } + + struct GenerateHashTestCase { + input: &'static str, + expected: &'static str, + } + + fn test_generate_hash_each(case: GenerateHashTestCase) { + let lockfile = Lockfile::new(case.input).unwrap(); + let hash = lockfile.generate_hash().unwrap(); + assert_eq!(hash, case.expected); + } + + test_each!( + test_generate_hash, + "npm" => GenerateHashTestCase { + input: "./tests/fixtures/lockfile/npm", + expected: "5hzH5KU3P+PfcvEwLVd5mIJrFInY5SfHCCeoPCspqUs=", + }, + "yarn" => GenerateHashTestCase { + input: "./tests/fixtures/lockfile/yarn", + expected: "dmy8HKrg+5lrLw8qnSanCzazm+5zgxA6la9Z2zh7GJ0=", + }, + "pnpm" => GenerateHashTestCase { + input: "./tests/fixtures/lockfile/pnpm", + expected: "3cxQHKge5hfKnZimYZiyfzdlbYSyanBOd8ImNQK7HCM=", + }, + "bun" => GenerateHashTestCase { + input: "./tests/fixtures/lockfile/bun", + expected: "hQM/8tFjhA/WkaV3dpQAEvNWVsyou1GpqyhIxwfKiTA=", + }, + ); +} diff --git a/src/utils/glob.rs b/src/utils/glob.rs index 68b6016..0185f8b 100644 --- a/src/utils/glob.rs +++ b/src/utils/glob.rs @@ -93,7 +93,7 @@ fn parse_negate(pattern: String, enable_negate: bool) -> (String, bool) { #[cfg(test)] mod tests { - use crate::test_each; + use crate::{test_each, test_each_serial}; use super::*; use std::fs::File; @@ -137,7 +137,7 @@ mod tests { tmp_dir.close().unwrap(); } - test_each! { + test_each_serial! { test_collect, 0 => &CollectTestCase { input: (vec!["foo"], true), diff --git a/src/utils/tests.rs b/src/utils/tests.rs index b590406..55d745a 100644 --- a/src/utils/tests.rs +++ b/src/utils/tests.rs @@ -1,6 +1,20 @@ mod tests { #[macro_export] macro_rules! test_each { + ($name:ident, $($suffix:expr => $case:expr,)*) => { + paste::item! { + $( + #[test] + fn [< $name _ $suffix >]() { + [< $name _each >]($case); + } + )* + } + }; + } + + #[macro_export] + macro_rules! test_each_serial { ($name:ident, $($suffix:expr => $case:expr,)*) => { paste::item! { $( diff --git a/tests/fixtures/lockfile/bun/bun.lockb b/tests/fixtures/lockfile/bun/bun.lockb new file mode 100755 index 0000000000000000000000000000000000000000..da2a398e37e9d3abc2743001c8ae960bf18c7b6b GIT binary patch literal 1322 zcmY#Z)GsYA(of3F(@)JSQ%EY!;{sycoc!eMw9K4T-L(9o+{6;yG6OCq1_p-k54)DF z+jTwLvvrBA_#=-8yPFq&@-h=Ckl))qonQFI`B^MLML+;j3j!Pv3Qjjb`Bg9lP(Fm> zVqkzNV3@DkprhTIn#72rf^-hdA~B#=8=(2DK$;nfi8T%624r`j^8fMwLzX1gPy*^0 z86f5&8_lw`bk5o}K9}A&c)#|&z__0!cJir}?bf~(>3&~=4%ID*zOhqe)rWlrgJT!5M)01bZBa41gAElVxZbFC;zEy_#G2`(wh%uBaZFhp2nfnrffaWdHN z2Y!nzEt)bx1eCEsV*K|O+q{!4+*MLsoLU4lG4`@TV^Es+4^SZT0NvGMVS0wKg72RL z)D@H9t^xVW5U00FDhpDJlZ!G7N+32tOwTAODJZtm*Dp#<&nzw}s?^IX$jvI&%P&gT XM`+O3MdIj!Lqo45y$bC7K}Y}qIj-0j literal 0 HcmV?d00001 diff --git a/tests/fixtures/lockfile/bun/package.json b/tests/fixtures/lockfile/bun/package.json new file mode 100644 index 0000000..7c88992 --- /dev/null +++ b/tests/fixtures/lockfile/bun/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "typescript": "^5.3.3" + } +} diff --git a/tests/fixtures/lockfile/nope/.gitkeep b/tests/fixtures/lockfile/nope/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/lockfile/npm/package-lock.json b/tests/fixtures/lockfile/npm/package-lock.json new file mode 100644 index 0000000..4bd1921 --- /dev/null +++ b/tests/fixtures/lockfile/npm/package-lock.json @@ -0,0 +1,24 @@ +{ + "name": "npm", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "typescript": "^5.3.3" + } + }, + "node_modules/typescript": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", + "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + } + } +} diff --git a/tests/fixtures/lockfile/npm/package.json b/tests/fixtures/lockfile/npm/package.json new file mode 100644 index 0000000..7c88992 --- /dev/null +++ b/tests/fixtures/lockfile/npm/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "typescript": "^5.3.3" + } +} diff --git a/tests/fixtures/lockfile/pnpm/package.json b/tests/fixtures/lockfile/pnpm/package.json new file mode 100644 index 0000000..7c88992 --- /dev/null +++ b/tests/fixtures/lockfile/pnpm/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "typescript": "^5.3.3" + } +} diff --git a/tests/fixtures/lockfile/pnpm/pnpm-lock.yaml b/tests/fixtures/lockfile/pnpm/pnpm-lock.yaml new file mode 100644 index 0000000..6813016 --- /dev/null +++ b/tests/fixtures/lockfile/pnpm/pnpm-lock.yaml @@ -0,0 +1,18 @@ +lockfileVersion: '6.0' + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + +dependencies: + typescript: + specifier: ^5.3.3 + version: 5.3.3 + +packages: + + /typescript@5.3.3: + resolution: {integrity: sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==} + engines: {node: '>=14.17'} + hasBin: true + dev: false diff --git a/tests/fixtures/lockfile/yarn/package.json b/tests/fixtures/lockfile/yarn/package.json new file mode 100644 index 0000000..7c88992 --- /dev/null +++ b/tests/fixtures/lockfile/yarn/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "typescript": "^5.3.3" + } +} diff --git a/tests/fixtures/lockfile/yarn/yarn.lock b/tests/fixtures/lockfile/yarn/yarn.lock new file mode 100644 index 0000000..cd07bce --- /dev/null +++ b/tests/fixtures/lockfile/yarn/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +typescript@^5.3.3: + version "5.3.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.3.3.tgz#b3ce6ba258e72e6305ba66f5c9b452aaee3ffe37" + integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==