Skip to content

Commit

Permalink
test: add some tests for lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
nokazn committed Feb 10, 2024
1 parent e93f268 commit 5088cac
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 3 deletions.
96 changes: 95 additions & 1 deletion src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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=",
},
);
}
4 changes: 2 additions & 2 deletions src/utils/glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -137,7 +137,7 @@ mod tests {
tmp_dir.close().unwrap();
}

test_each! {
test_each_serial! {
test_collect,
0 => &CollectTestCase {
input: (vec!["foo"], true),
Expand Down
14 changes: 14 additions & 0 deletions src/utils/tests.rs
Original file line number Diff line number Diff line change
@@ -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! {
$(
Expand Down
Binary file added tests/fixtures/lockfile/bun/bun.lockb
Binary file not shown.
5 changes: 5 additions & 0 deletions tests/fixtures/lockfile/bun/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"typescript": "^5.3.3"
}
}
Empty file.
24 changes: 24 additions & 0 deletions tests/fixtures/lockfile/npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/fixtures/lockfile/npm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"typescript": "^5.3.3"
}
}
5 changes: 5 additions & 0 deletions tests/fixtures/lockfile/pnpm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"typescript": "^5.3.3"
}
}
18 changes: 18 additions & 0 deletions tests/fixtures/lockfile/pnpm/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/fixtures/lockfile/yarn/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"typescript": "^5.3.3"
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/lockfile/yarn/yarn.lock
Original file line number Diff line number Diff line change
@@ -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==

0 comments on commit 5088cac

Please sign in to comment.