From 753b3f95c1ba356d21970510ea27398e332ee829 Mon Sep 17 00:00:00 2001 From: hsjobeki Date: Mon, 11 Sep 2023 13:15:33 +0200 Subject: [PATCH 1/6] cleanup work --- lib/internal/nodejsLockUtils.nix | 29 +++++++- .../nodejs-package-lock-v3/default.nix | 60 ++++++++++------ .../nodejs-package-lock-v3/interface.nix | 71 ++++++------------- .../nix-unit/test_nodejs_lock_v3/default.nix | 45 ++++++------ .../test_nodejs_lockutils/default.nix | 1 + 5 files changed, 108 insertions(+), 98 deletions(-) diff --git a/lib/internal/nodejsLockUtils.nix b/lib/internal/nodejsLockUtils.nix index a82cdfea6e..6c4d6e22f3 100644 --- a/lib/internal/nodejsLockUtils.nix +++ b/lib/internal/nodejsLockUtils.nix @@ -23,6 +23,33 @@ else if currentPath == "" then throw "${search} not found in package-lock.json." else findEntry packageLock (stripPath currentPath) search; + + # Returns the names of all "bundledDependencies". + # People depend on different types and different names. Unfortunatly those fields are not part of the offical npm documentation. + # Which may also be the reason for the mess. + # + # TODO: define unit tests. + # Adopted from https://github.com/aakropotkin/floco/blob/708c4ffa0c05033c29fe6886a238cb20c3ba3fb4/modules/plock/implementation.nix#L139 + # + # getBundledDependencies :: Pent -> {} + getBundledDependencies = pent: let + # b :: bool | [] + b = pent.bundledDependencies or pent.bundleDependencies or []; + in + # The following asserts is the XOR logic. + # "bundle" and "bundled" dependencies are both valid but invalid if both or none keys exist + assert ( pent ? bundledDependencies ) -> + ( ! ( pent ? bundleDependencies ) ); + assert ( pent ? bundleDependencies ) -> + ( ! ( pent ? bundledDependencies ) ); + if b == [] then {} else + if builtins.isList b then { bundledDependencies = b; } else + if ! b then {} else { + # b :: true + bundledDependencies = builtins.attrNames ( + ( pent.dependencies or {} ) // ( pent.requires or {} ) + ); + }; in { - inherit findEntry stripPath; + inherit findEntry stripPath getBundledDependencies; } diff --git a/modules/dream2nix/nodejs-package-lock-v3/default.nix b/modules/dream2nix/nodejs-package-lock-v3/default.nix index 07859899fd..d558fb157c 100644 --- a/modules/dream2nix/nodejs-package-lock-v3/default.nix +++ b/modules/dream2nix/nodejs-package-lock-v3/default.nix @@ -9,33 +9,47 @@ inherit (config.deps) fetchurl; - nodejsLockUtils = import ../../../lib/internal/nodejsLockUtils.nix {inherit lib;}; + nodejsLockUtils = import ../../../lib/internal/nodejsLockUtils.nix { inherit lib; }; - isLink = plent: plent ? link && plent.link; + # Collection of sanitized functions that always return the same type + isLink = pent: pent.link or false; - parseSource = plent: - if isLink plent + # isDev = pent: pent.dev or false; + # isOptional = pent: pent.optional or false; + # isInBundle = pent: pent.inBundle or false; + # hasInstallScript = pent: pent.hasInstallScript or false; + # getBin = pent: pent.bin or {}; + + /* + Pent :: { + See: https://docs.npmjs.com/cli/v9/configuring-npm/package-lock-json#packages + } + pent is one entry of 'packages' + */ + parseSource = pent: + if isLink pent then # entry is local file - (builtins.dirOf config.nodejs-package-lock-v3.packageLockFile) + "/${plent.resolved}" + (builtins.dirOf config.nodejs-package-lock-v3.packageLockFile) + "/${pent.resolved}" else fetchurl { - url = plent.resolved; - hash = plent.integrity; + url = pent.resolved; + hash = pent.integrity; }; - getDependencies = lock: path: plent: - l.mapAttrs (name: _descriptor: { - dev = plent.dev or false; - version = let - # Need this util as dependencies no explizitly locked version - # This findEntry is needed to find the exact locked version - packageIdent = nodejsLockUtils.findEntry lock path name; - in - # Read version from package-lock entry for the resolved package - lock.packages.${packageIdent}.version; + + getDependencies = lock: path: pent: + l.mapAttrs (depName: _semverConstraint: + let + packageIdent = nodejsLockUtils.findEntry lock path depName; + depPent = lock.packages.${packageIdent}; + in + { + dev = pent.dev or false; + version = depPent.version; }) - (plent.dependencies or {} // plent.devDependencies or {} // plent.optionalDependencies or {}); + (pent.dependencies or {} // pent.devDependencies or {} // pent.optionalDependencies or {}); + # Takes one entry of "package" from package-lock.json parseEntry = lock: path: entry: @@ -70,15 +84,17 @@ }; }; - parse = lock: - builtins.foldl' + mergePdefs = builtins.foldl' (acc: entry: acc // { ${entry.name} = acc.${entry.name} or {} // entry.value; }) - {} - # [{name=; value=;} ...] + {}; + + parse = lock: + mergePdefs + # type: [ { name :: String; value :: {...}; } ] (l.mapAttrsToList (parseEntry lock) lock.packages); pdefs = parse config.nodejs-package-lock-v3.packageLock; diff --git a/modules/dream2nix/nodejs-package-lock-v3/interface.nix b/modules/dream2nix/nodejs-package-lock-v3/interface.nix index e045273dba..eb0001ae83 100644 --- a/modules/dream2nix/nodejs-package-lock-v3/interface.nix +++ b/modules/dream2nix/nodejs-package-lock-v3/interface.nix @@ -1,7 +1,3 @@ -# subsystemAttrs :: { -# meta? :: { -# } -# } { config, options, @@ -16,7 +12,7 @@ derivationType = t.oneOf [t.str t.path t.package]; - # A stricteer submodule type that prevents derivations from being + # A stricter submodule type that prevents derivations from being # detected as modules by accident. (derivations are attrs as well as modules) drvPart = let type = t.submoduleWith { @@ -72,20 +68,25 @@ in { description = "The content of the package-lock.json"; }; - # pdefs.${name}.${version} :: { - # // all dependency entries of that package. - # // each dependency is guaranteed to have its own entry in 'pdef' - # // A package without dependencies has `dependencies = {}` (So dependencies has a constant type) - # dependencies = { - # ${name} = { - # dev = boolean; - # version :: string; - # } - # } - # // Pointing to the source of the package. - # // in most cases this is a tarball (tar.gz) which needs to be unpacked by e.g. unpackPhase - # source :: Derivation | Path - # } + /* + + type: pdefs.${name}.${version} :: { + + // Pointing to the source of the package. + // in most cases this is a tarball (tar.gz) which needs to be unpacked by e.g. unpackPhase + source :: Derivation | Path + + // all dependency entries of that package. + // each dependency is guaranteed to have its own entry in 'pdef' + // A package without dependencies has `dependencies = {}` (Empty set) + dependencies = { + ${name} = { + dev = boolean; + version :: string; + } + } + } + */ pdefs = { type = t.attrsOf (t.attrsOf (t.submodule { options.dependencies = l.mkOption { @@ -94,37 +95,5 @@ in { options.source = optPackage; })); }; - - # packageJsonFile = { - # type = t.path; - # description = '' - # The package.json file to use. - # ''; - # default = cfg.source + "/package.json"; - # }; - # packageJson = { - # type = t.attrs; - # description = "The content of the package.json"; - # }; - # source = { - # type = t.either t.path t.package; - # description = "Source of the package"; - # default = config.mkDerivation.src; - # }; - # withDevDependencies = { - # type = t.bool; - # default = true; - # description = '' - # Whether to include development dependencies. - # Usually it's a bad idea to disable this, as development dependencies can contain important build time dependencies. - # ''; - # }; - # workspaces = { - # type = t.listOf t.str; - # description = '' - # Workspaces to include. - # Defaults to the ones defined in package.json. - # ''; - # }; }; } diff --git a/tests/nix-unit/test_nodejs_lock_v3/default.nix b/tests/nix-unit/test_nodejs_lock_v3/default.nix index 9e938604e4..858188f826 100644 --- a/tests/nix-unit/test_nodejs_lock_v3/default.nix +++ b/tests/nix-unit/test_nodejs_lock_v3/default.nix @@ -68,18 +68,13 @@ in { "resolved" = "https://registry.npmjs.org/async/-/async-0.2.10.tgz"; "integrity" = "sha512-eAkdoKxU6/LkKDBzLpT+t6Ff5EtfSF4wx1WfJiPEEV7WNLnDaRXk0oVysiEPm262roaachGexwUv94WhSgN5TQ=="; }; - # "node_modules/@org/async" = { - # "version" = "0.2.10"; - # "resolved" = "https://registry.npmjs.org/async/-/async-0.2.10.tgz"; - # "integrity" = "sha512-eAkdoKxU6/LkKDBzLpT+t6Ff5EtfSF4wx1WfJiPEEV7WNLnDaRXk0oVysiEPm262roaachGexwUv94WhSgN5TQ=="; - # }; }; }; }; config = evaled.config; in { - expr = config.nodejs-package-lock-v3.pdefs."async"."0.2.10".source.type; - expected = "derivation"; + expr = "${config.nodejs-package-lock-v3.pdefs."async"."0.2.10".source}"; + expected = "/nix/store/sm4v0qaynkjf704lrcqxhlssp003y9h8-async-0.2.10.tgz"; }; # test if dependencies are ignored successfully in pip.rootDependencies @@ -297,21 +292,23 @@ in { ]; }; - # TODO: some infinite recursion occurs when accessing pdef.{name}.{version}.source - # test_nodejs_parse_lockfile = let - # evaled = eval { - # imports = [ - # dream2nix.modules.dream2nix.nodejs-package-lock-v3 - # ]; - # nodejs-package-lock-v3.packageLockFile = ./package-lock.json; - # nodejs-package-lock-v3.packageLock = lib.mkForce (builtins.fromJSON (builtins.readFile ./package-lock.json)); - # # set the root package source - # nodejs-package-lock-v3.pdefs."minimal"."1.0.0".source = ""; - # }; - # config = evaled.config; - # in { - # expr = config.nodejs-package-lock-v3.pdefs."argparse"."0.1.16"; - # expected = { - # }; - # }; + test_nodejs_wrong_lockfile = let + evaled = eval { + imports = [ + dream2nix.modules.dream2nix.nodejs-package-lock-v3 + ]; + nodejs-package-lock-v3.packageLock = lib.mkForce { + # Example content of lockfile + "lockfileVersion" = 1; + }; + }; + config = evaled.config; + in { + expr = builtins.tryEval (config.nodejs-package-lock-v3.pdefs); + expected = { + success = false; + value = false; + }; + }; + } diff --git a/tests/nix-unit/test_nodejs_lockutils/default.nix b/tests/nix-unit/test_nodejs_lockutils/default.nix index 9c5ce37ba8..fbcb70465d 100644 --- a/tests/nix-unit/test_nodejs_lockutils/default.nix +++ b/tests/nix-unit/test_nodejs_lockutils/default.nix @@ -74,4 +74,5 @@ expr = path; expected = "node_modules/underscore"; }; + } From efe16f811ab39a5a4cd98a698ea1230122b994a7 Mon Sep 17 00:00:00 2001 From: hsjobeki Date: Mon, 11 Sep 2023 13:20:06 +0200 Subject: [PATCH 2/6] add basic lockfile check --- modules/dream2nix/nodejs-package-lock-v3/default.nix | 2 ++ tests/nix-unit/test_nodejs_lock_v3/default.nix | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/dream2nix/nodejs-package-lock-v3/default.nix b/modules/dream2nix/nodejs-package-lock-v3/default.nix index d558fb157c..d7c7b4d84a 100644 --- a/modules/dream2nix/nodejs-package-lock-v3/default.nix +++ b/modules/dream2nix/nodejs-package-lock-v3/default.nix @@ -93,6 +93,8 @@ {}; parse = lock: + assert lock.lockfileVersion != 1; + assert lock ? packages; mergePdefs # type: [ { name :: String; value :: {...}; } ] (l.mapAttrsToList (parseEntry lock) lock.packages); diff --git a/tests/nix-unit/test_nodejs_lock_v3/default.nix b/tests/nix-unit/test_nodejs_lock_v3/default.nix index 858188f826..1b4f312f59 100644 --- a/tests/nix-unit/test_nodejs_lock_v3/default.nix +++ b/tests/nix-unit/test_nodejs_lock_v3/default.nix @@ -292,7 +292,7 @@ in { ]; }; - test_nodejs_wrong_lockfile = let + test_nodejs_wrong_lockfile_version = let evaled = eval { imports = [ dream2nix.modules.dream2nix.nodejs-package-lock-v3 From 9233de86a46c99d26e78200137c15b3d12d7c163 Mon Sep 17 00:00:00 2001 From: hsjobeki Date: Mon, 11 Sep 2023 13:29:40 +0200 Subject: [PATCH 3/6] add sanitizeLockfile smoke validator --- lib/internal/nodejsLockUtils.nix | 13 ++++++++++++- .../dream2nix/nodejs-package-lock-v3/default.nix | 4 +--- tests/nix-unit/test_nodejs_lock_v3/default.nix | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/internal/nodejsLockUtils.nix b/lib/internal/nodejsLockUtils.nix index 6c4d6e22f3..f6cdb09303 100644 --- a/lib/internal/nodejsLockUtils.nix +++ b/lib/internal/nodejsLockUtils.nix @@ -7,6 +7,17 @@ in lib.removeSuffix "/" nextPath; + sanitizeLockfile = lock: + # Every project MUST have a name + assert lock ? name; + # Every project MUST have a version + assert lock ? version; + # This lockfile module only supports lockfileVersion 2 and 3 + assert !lock ? lockfileVersion || lock.lockfileVersion >= 2; + # The Lockfile must contain a 'packages' attribute. + assert lock ? packages; + lock; + findEntry = # = "attrs" packageLock: @@ -51,5 +62,5 @@ ); }; in { - inherit findEntry stripPath getBundledDependencies; + inherit findEntry stripPath getBundledDependencies sanitizeLockfile; } diff --git a/modules/dream2nix/nodejs-package-lock-v3/default.nix b/modules/dream2nix/nodejs-package-lock-v3/default.nix index d7c7b4d84a..1541bb09f3 100644 --- a/modules/dream2nix/nodejs-package-lock-v3/default.nix +++ b/modules/dream2nix/nodejs-package-lock-v3/default.nix @@ -93,13 +93,11 @@ {}; parse = lock: - assert lock.lockfileVersion != 1; - assert lock ? packages; mergePdefs # type: [ { name :: String; value :: {...}; } ] (l.mapAttrsToList (parseEntry lock) lock.packages); - pdefs = parse config.nodejs-package-lock-v3.packageLock; + pdefs = parse (nodejsLockUtils.sanitizeLockfile config.nodejs-package-lock-v3.packageLock); in { imports = [ ./interface.nix diff --git a/tests/nix-unit/test_nodejs_lock_v3/default.nix b/tests/nix-unit/test_nodejs_lock_v3/default.nix index 1b4f312f59..f10307b93f 100644 --- a/tests/nix-unit/test_nodejs_lock_v3/default.nix +++ b/tests/nix-unit/test_nodejs_lock_v3/default.nix @@ -299,7 +299,7 @@ in { ]; nodejs-package-lock-v3.packageLock = lib.mkForce { # Example content of lockfile - "lockfileVersion" = 1; + # "lockfileVersion" = 1; }; }; config = evaled.config; From 267ed15e5c7004e27f723dc46bef52d51e39c185 Mon Sep 17 00:00:00 2001 From: hsjobeki Date: Mon, 11 Sep 2023 14:19:09 +0200 Subject: [PATCH 4/6] add lockfile tests --- .../test_nodejs_lockutils/default.nix | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/nix-unit/test_nodejs_lockutils/default.nix b/tests/nix-unit/test_nodejs_lockutils/default.nix index fbcb70465d..4fc5cd3787 100644 --- a/tests/nix-unit/test_nodejs_lockutils/default.nix +++ b/tests/nix-unit/test_nodejs_lockutils/default.nix @@ -75,4 +75,42 @@ expected = "node_modules/underscore"; }; + # test the lock + test_nodejsLockUtils_lockfile_v3 = let + plock = { + name = "foo"; + version = "1.0.0"; + lockfileVersion = 3; + packages = {}; + }; + in { + expr = nodejsLockUtils.sanitizeLockfile plock; + expected = plock; + }; + + test_nodejsLockUtils_lockfile_v2 = let + plock = { + name = "foo"; + version = "1.0.0"; + lockfileVersion = 2; + packages = {}; + dependencies = {}; + }; + in { + expr = nodejsLockUtils.sanitizeLockfile plock; + expected = plock; + }; + + test_nodejsLockUtils_lockfile_v1 = let + plock = { + name = "foo"; + version = "1.0.0"; + lockfileVersion = 1; + dependencies = {}; + }; + in { + expr = nodejsLockUtils.sanitizeLockfile plock; + expectedError = plock; + }; + } From 2760d68d31374546fd83f7d2a3c858b4cc018382 Mon Sep 17 00:00:00 2001 From: hsjobeki Date: Fri, 13 Oct 2023 10:19:51 +0200 Subject: [PATCH 5/6] add more unit tests for catching invalid lockfiles early --- lib/internal/nodejsLockUtils.nix | 52 +++++--------- .../nodejs-package-lock-v3/default.nix | 22 +++--- .../nodejs-package-lock-v3/interface.nix | 4 +- .../nix-unit/test_nodejs_lock_v3/default.nix | 16 ++--- .../test_nodejs_lockutils/default.nix | 68 ++++++++++++++++++- 5 files changed, 100 insertions(+), 62 deletions(-) diff --git a/lib/internal/nodejsLockUtils.nix b/lib/internal/nodejsLockUtils.nix index f6cdb09303..cf5f4718dd 100644 --- a/lib/internal/nodejsLockUtils.nix +++ b/lib/internal/nodejsLockUtils.nix @@ -9,14 +9,21 @@ sanitizeLockfile = lock: # Every project MUST have a name - assert lock ? name; - # Every project MUST have a version - assert lock ? version; - # This lockfile module only supports lockfileVersion 2 and 3 - assert !lock ? lockfileVersion || lock.lockfileVersion >= 2; - # The Lockfile must contain a 'packages' attribute. - assert lock ? packages; - lock; + if ! lock ? name + then throw "Invalid lockfile: Every project MUST have a name" + else + # Every project MUST have a version + if ! lock ? version + then throw "Invalid lockfile: Every project MUST have a version" + else + # This lockfile module only supports lockfileVersion 2 and 3 + if ! lock ? lockfileVersion || lock.lockfileVersion <= 1 + then throw "This lockfile module only supports lockfileVersion 2 and 3" + else + # The Lockfile must contain a 'packages' attribute. + if ! lock ? packages + then throw "Invalid lockfile: The Lockfile must contain 'packages' attribute." + else lock; findEntry = # = "attrs" @@ -34,33 +41,6 @@ else if currentPath == "" then throw "${search} not found in package-lock.json." else findEntry packageLock (stripPath currentPath) search; - - # Returns the names of all "bundledDependencies". - # People depend on different types and different names. Unfortunatly those fields are not part of the offical npm documentation. - # Which may also be the reason for the mess. - # - # TODO: define unit tests. - # Adopted from https://github.com/aakropotkin/floco/blob/708c4ffa0c05033c29fe6886a238cb20c3ba3fb4/modules/plock/implementation.nix#L139 - # - # getBundledDependencies :: Pent -> {} - getBundledDependencies = pent: let - # b :: bool | [] - b = pent.bundledDependencies or pent.bundleDependencies or []; - in - # The following asserts is the XOR logic. - # "bundle" and "bundled" dependencies are both valid but invalid if both or none keys exist - assert ( pent ? bundledDependencies ) -> - ( ! ( pent ? bundleDependencies ) ); - assert ( pent ? bundleDependencies ) -> - ( ! ( pent ? bundledDependencies ) ); - if b == [] then {} else - if builtins.isList b then { bundledDependencies = b; } else - if ! b then {} else { - # b :: true - bundledDependencies = builtins.attrNames ( - ( pent.dependencies or {} ) // ( pent.requires or {} ) - ); - }; in { - inherit findEntry stripPath getBundledDependencies sanitizeLockfile; + inherit findEntry stripPath sanitizeLockfile; } diff --git a/modules/dream2nix/nodejs-package-lock-v3/default.nix b/modules/dream2nix/nodejs-package-lock-v3/default.nix index 1541bb09f3..e8828c7708 100644 --- a/modules/dream2nix/nodejs-package-lock-v3/default.nix +++ b/modules/dream2nix/nodejs-package-lock-v3/default.nix @@ -9,7 +9,7 @@ inherit (config.deps) fetchurl; - nodejsLockUtils = import ../../../lib/internal/nodejsLockUtils.nix { inherit lib; }; + nodejsLockUtils = import ../../../lib/internal/nodejsLockUtils.nix {inherit lib;}; # Collection of sanitized functions that always return the same type isLink = pent: pent.link or false; @@ -21,10 +21,11 @@ # getBin = pent: pent.bin or {}; /* - Pent :: { - See: https://docs.npmjs.com/cli/v9/configuring-npm/package-lock-json#packages - } - pent is one entry of 'packages' + Pent :: { + See: https://docs.npmjs.com/cli/v9/configuring-npm/package-lock-json#packages + } + > We should mention that docs are imcomplete on npmjs.com + pent is one entry of 'packages' */ parseSource = pent: if isLink pent @@ -37,20 +38,16 @@ hash = pent.integrity; }; - getDependencies = lock: path: pent: - l.mapAttrs (depName: _semverConstraint: - let + l.mapAttrs (depName: _semverConstraint: let packageIdent = nodejsLockUtils.findEntry lock path depName; depPent = lock.packages.${packageIdent}; - in - { + in { dev = pent.dev or false; version = depPent.version; }) (pent.dependencies or {} // pent.devDependencies or {} // pent.optionalDependencies or {}); - # Takes one entry of "package" from package-lock.json parseEntry = lock: path: entry: if path == "" @@ -84,7 +81,8 @@ }; }; - mergePdefs = builtins.foldl' + mergePdefs = + builtins.foldl' (acc: entry: acc // { diff --git a/modules/dream2nix/nodejs-package-lock-v3/interface.nix b/modules/dream2nix/nodejs-package-lock-v3/interface.nix index eb0001ae83..ab87720b73 100644 --- a/modules/dream2nix/nodejs-package-lock-v3/interface.nix +++ b/modules/dream2nix/nodejs-package-lock-v3/interface.nix @@ -71,7 +71,7 @@ in { /* type: pdefs.${name}.${version} :: { - + // Pointing to the source of the package. // in most cases this is a tarball (tar.gz) which needs to be unpacked by e.g. unpackPhase source :: Derivation | Path @@ -86,7 +86,7 @@ in { } } } - */ + */ pdefs = { type = t.attrsOf (t.attrsOf (t.submodule { options.dependencies = l.mkOption { diff --git a/tests/nix-unit/test_nodejs_lock_v3/default.nix b/tests/nix-unit/test_nodejs_lock_v3/default.nix index f10307b93f..f51976e7cd 100644 --- a/tests/nix-unit/test_nodejs_lock_v3/default.nix +++ b/tests/nix-unit/test_nodejs_lock_v3/default.nix @@ -297,18 +297,16 @@ in { imports = [ dream2nix.modules.dream2nix.nodejs-package-lock-v3 ]; - nodejs-package-lock-v3.packageLock = lib.mkForce { - # Example content of lockfile - # "lockfileVersion" = 1; - }; + nodejs-package-lock-v3.packageLock = + lib.mkForce { + }; }; config = evaled.config; in { - expr = builtins.tryEval (config.nodejs-package-lock-v3.pdefs); - expected = { - success = false; - value = false; + expr = config.nodejs-package-lock-v3.pdefs; + expectedError = { + type = "ThrownError"; + msg = "Invalid lockfile"; }; }; - } diff --git a/tests/nix-unit/test_nodejs_lockutils/default.nix b/tests/nix-unit/test_nodejs_lockutils/default.nix index 4fc5cd3787..3e38cf006e 100644 --- a/tests/nix-unit/test_nodejs_lockutils/default.nix +++ b/tests/nix-unit/test_nodejs_lockutils/default.nix @@ -74,8 +74,8 @@ expr = path; expected = "node_modules/underscore"; }; - - # test the lock + + # test the lock test_nodejsLockUtils_lockfile_v3 = let plock = { name = "foo"; @@ -110,7 +110,69 @@ }; in { expr = nodejsLockUtils.sanitizeLockfile plock; - expectedError = plock; + expectedError = { + type = "ThrownError"; + msg = "This lockfile module only supports lockfileVersion 2 and 3"; + }; }; + test_nodejsLockUtils_lockfile_missing_name = let + plock = { + # name = "foo"; + version = "1.0.0"; + lockfileVersion = 3; + packages = {}; + }; + in { + expr = nodejsLockUtils.sanitizeLockfile plock; + expectedError = { + type = "ThrownError"; + msg = "MUST have a name"; + }; + }; + + test_nodejsLockUtils_lockfile_missing_version = let + plock = { + name = "foo"; + # version = "1.0.0"; + lockfileVersion = 3; + packages = {}; + }; + in { + expr = nodejsLockUtils.sanitizeLockfile plock; + expectedError = { + type = "ThrownError"; + msg = "MUST have a version"; + }; + }; + + test_nodejsLockUtils_lockfile_missing_lockfileVersion = let + plock = { + name = "foo"; + version = "1.0.0"; + # lockfileVersion = 3; + packages = {}; + }; + in { + expr = nodejsLockUtils.sanitizeLockfile plock; + expectedError = { + type = "ThrownError"; + msg = "lockfileVersion"; + }; + }; + + test_nodejsLockUtils_lockfile_missing_packages = let + plock = { + name = "foo"; + version = "1.0.0"; + lockfileVersion = 3; + # packages = {}; + }; + in { + expr = nodejsLockUtils.sanitizeLockfile plock; + expectedError = { + type = "ThrownError"; + msg = "must contain 'packages'"; + }; + }; } From 4c58eeb0aeea9d824319932c0bd0691b8cb2f938 Mon Sep 17 00:00:00 2001 From: hsjobeki Date: Tue, 17 Oct 2023 22:30:14 +0200 Subject: [PATCH 6/6] just check the lockfileVersion --- lib/internal/nodejsLockUtils.nix | 20 ++------- .../test_nodejs_lockutils/default.nix | 45 ------------------- 2 files changed, 4 insertions(+), 61 deletions(-) diff --git a/lib/internal/nodejsLockUtils.nix b/lib/internal/nodejsLockUtils.nix index cf5f4718dd..ba96e3e325 100644 --- a/lib/internal/nodejsLockUtils.nix +++ b/lib/internal/nodejsLockUtils.nix @@ -8,22 +8,10 @@ lib.removeSuffix "/" nextPath; sanitizeLockfile = lock: - # Every project MUST have a name - if ! lock ? name - then throw "Invalid lockfile: Every project MUST have a name" - else - # Every project MUST have a version - if ! lock ? version - then throw "Invalid lockfile: Every project MUST have a version" - else - # This lockfile module only supports lockfileVersion 2 and 3 - if ! lock ? lockfileVersion || lock.lockfileVersion <= 1 - then throw "This lockfile module only supports lockfileVersion 2 and 3" - else - # The Lockfile must contain a 'packages' attribute. - if ! lock ? packages - then throw "Invalid lockfile: The Lockfile must contain 'packages' attribute." - else lock; + # This lockfile module only supports lockfileVersion 2 and 3 + if ! lock ? lockfileVersion || lock.lockfileVersion <= 1 + then throw "This lockfile module only supports lockfileVersion 2 and 3" + else lock; findEntry = # = "attrs" diff --git a/tests/nix-unit/test_nodejs_lockutils/default.nix b/tests/nix-unit/test_nodejs_lockutils/default.nix index 3e38cf006e..f5de80f163 100644 --- a/tests/nix-unit/test_nodejs_lockutils/default.nix +++ b/tests/nix-unit/test_nodejs_lockutils/default.nix @@ -116,36 +116,6 @@ }; }; - test_nodejsLockUtils_lockfile_missing_name = let - plock = { - # name = "foo"; - version = "1.0.0"; - lockfileVersion = 3; - packages = {}; - }; - in { - expr = nodejsLockUtils.sanitizeLockfile plock; - expectedError = { - type = "ThrownError"; - msg = "MUST have a name"; - }; - }; - - test_nodejsLockUtils_lockfile_missing_version = let - plock = { - name = "foo"; - # version = "1.0.0"; - lockfileVersion = 3; - packages = {}; - }; - in { - expr = nodejsLockUtils.sanitizeLockfile plock; - expectedError = { - type = "ThrownError"; - msg = "MUST have a version"; - }; - }; - test_nodejsLockUtils_lockfile_missing_lockfileVersion = let plock = { name = "foo"; @@ -160,19 +130,4 @@ msg = "lockfileVersion"; }; }; - - test_nodejsLockUtils_lockfile_missing_packages = let - plock = { - name = "foo"; - version = "1.0.0"; - lockfileVersion = 3; - # packages = {}; - }; - in { - expr = nodejsLockUtils.sanitizeLockfile plock; - expectedError = { - type = "ThrownError"; - msg = "must contain 'packages'"; - }; - }; }