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 0000000..da2a398 Binary files /dev/null and b/tests/fixtures/lockfile/bun/bun.lockb differ 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==