From 19c2f71da6b81e32ee2848bce6e9fe9f5d92d9f1 Mon Sep 17 00:00:00 2001 From: ghe Date: Mon, 20 Feb 2023 18:28:00 +0000 Subject: [PATCH 1/2] fix: npm version parse test & enable logging --- lib/utils.ts | 5 +++++ test/fixtures/bare-npm/package-lock.json | 5 +++++ test/fixtures/bare-npm/package.json | 13 +++++++++++++ .../__snapshots__/get-lockfile-version.test.ts.snap | 3 +++ test/jest/get-lockfile-version.test.ts | 10 ++++++++++ 5 files changed, 36 insertions(+) create mode 100644 test/fixtures/bare-npm/package-lock.json create mode 100644 test/fixtures/bare-npm/package.json create mode 100644 test/jest/__snapshots__/get-lockfile-version.test.ts.snap create mode 100644 test/jest/get-lockfile-version.test.ts diff --git a/lib/utils.ts b/lib/utils.ts index a2fdf291..c7511bbc 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,5 +1,8 @@ import { readFileSync } from 'fs'; import { InvalidUserInputError } from './errors'; +import * as baseDebug from 'debug'; + +const debug = baseDebug('snyk-nodejs-parser'); export enum NodeLockfileVersion { NpmLockV1 = 'NPM_LOCK_V1', @@ -43,6 +46,7 @@ export function getNpmLockfileVersion( | NodeLockfileVersion.NpmLockV3 { try { const lockfileJson = JSON.parse(lockFileContents); + const lockfileVersion: number | null = lockfileJson.lockfileVersion || null; switch (lockfileVersion) { @@ -60,6 +64,7 @@ export function getNpmLockfileVersion( ); } } catch (e) { + debug('Failed to parse lockfile version: ', e); throw new InvalidUserInputError( `Problem parsing package-lock.json - make sure the package-lock.json is a valid JSON file`, ); diff --git a/test/fixtures/bare-npm/package-lock.json b/test/fixtures/bare-npm/package-lock.json new file mode 100644 index 00000000..3a65c136 --- /dev/null +++ b/test/fixtures/bare-npm/package-lock.json @@ -0,0 +1,5 @@ +{ + "name": "Example", + "version": "1.0.0", + "lockfileVersion": 1 +} diff --git a/test/fixtures/bare-npm/package.json b/test/fixtures/bare-npm/package.json new file mode 100644 index 00000000..16c8bc63 --- /dev/null +++ b/test/fixtures/bare-npm/package.json @@ -0,0 +1,13 @@ +{ + "name": "Example", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "Example", + "version": "1.0.0" + } + } +} + diff --git a/test/jest/__snapshots__/get-lockfile-version.test.ts.snap b/test/jest/__snapshots__/get-lockfile-version.test.ts.snap new file mode 100644 index 00000000..0c273f82 --- /dev/null +++ b/test/jest/__snapshots__/get-lockfile-version.test.ts.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`getLockfileVersionFromFile npm 1`] = `"NPM_LOCK_V1"`; diff --git a/test/jest/get-lockfile-version.test.ts b/test/jest/get-lockfile-version.test.ts new file mode 100644 index 00000000..51f27538 --- /dev/null +++ b/test/jest/get-lockfile-version.test.ts @@ -0,0 +1,10 @@ +import { getLockfileVersionFromFile } from '../../lib/utils'; + +describe('getLockfileVersionFromFile', () => { + it('npm', () => { + const result = getLockfileVersionFromFile( + `${__dirname}/../fixtures/bare-npm/package-lock.json`, + ); + expect(result).toMatchSnapshot(); + }); +}); From 7b996255b8cce5823e8e249973d6677c771fb912 Mon Sep 17 00:00:00 2001 From: ghe Date: Mon, 20 Feb 2023 19:03:02 +0000 Subject: [PATCH 2/2] chore: enable Node 12 tests since CLI still supports it --- .circleci/config.yml | 20 +++++++++++++++++++ lib/utils.ts | 5 ----- test/fixtures/bare-npm/package-lock.json | 13 +++++++++--- test/fixtures/bare-npm/package.json | 13 ++---------- .../get-lockfile-version.test.ts.snap | 2 +- 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index b45fb003..b43bf30d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -159,6 +159,26 @@ workflows: branches: ignore: - master + - test-windows: + name: Windows Tests for Node v12 support + context: nodejs-install + node_version: "12.22.12" + requires: + - Lint + filters: + branches: + ignore: + - master + - test-unix: + name: Unix Tests for Node v12 support + context: nodejs-install + node_version: "12.22.12" + requires: + - Lint + filters: + branches: + ignore: + - master - release: name: Release context: nodejs-app-release diff --git a/lib/utils.ts b/lib/utils.ts index c7511bbc..a2fdf291 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,8 +1,5 @@ import { readFileSync } from 'fs'; import { InvalidUserInputError } from './errors'; -import * as baseDebug from 'debug'; - -const debug = baseDebug('snyk-nodejs-parser'); export enum NodeLockfileVersion { NpmLockV1 = 'NPM_LOCK_V1', @@ -46,7 +43,6 @@ export function getNpmLockfileVersion( | NodeLockfileVersion.NpmLockV3 { try { const lockfileJson = JSON.parse(lockFileContents); - const lockfileVersion: number | null = lockfileJson.lockfileVersion || null; switch (lockfileVersion) { @@ -64,7 +60,6 @@ export function getNpmLockfileVersion( ); } } catch (e) { - debug('Failed to parse lockfile version: ', e); throw new InvalidUserInputError( `Problem parsing package-lock.json - make sure the package-lock.json is a valid JSON file`, ); diff --git a/test/fixtures/bare-npm/package-lock.json b/test/fixtures/bare-npm/package-lock.json index 3a65c136..cd96cdfc 100644 --- a/test/fixtures/bare-npm/package-lock.json +++ b/test/fixtures/bare-npm/package-lock.json @@ -1,5 +1,12 @@ { - "name": "Example", - "version": "1.0.0", - "lockfileVersion": 1 + "name": "bare-npm", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "bare-npm", + "version": "1.0.0" + } + } } diff --git a/test/fixtures/bare-npm/package.json b/test/fixtures/bare-npm/package.json index 16c8bc63..f2fb8633 100644 --- a/test/fixtures/bare-npm/package.json +++ b/test/fixtures/bare-npm/package.json @@ -1,13 +1,4 @@ { - "name": "Example", - "version": "1.0.0", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "name": "Example", - "version": "1.0.0" - } - } + "name": "bare-npm", + "version": "1.0.0" } - diff --git a/test/jest/__snapshots__/get-lockfile-version.test.ts.snap b/test/jest/__snapshots__/get-lockfile-version.test.ts.snap index 0c273f82..aae10e0c 100644 --- a/test/jest/__snapshots__/get-lockfile-version.test.ts.snap +++ b/test/jest/__snapshots__/get-lockfile-version.test.ts.snap @@ -1,3 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`getLockfileVersionFromFile npm 1`] = `"NPM_LOCK_V1"`; +exports[`getLockfileVersionFromFile npm 1`] = `"NPM_LOCK_V2"`;