From 2ba6ce344ad91b0b1bf5c34890468b3d75200933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 22 Mar 2022 11:51:55 +0200 Subject: [PATCH 1/9] Add @types/semver dev dependency --- package.json | 1 + yarn.lock | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/package.json b/package.json index 8147a4e..3f4eef4 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "devDependencies": { "@types/jest": "^19.2.4", "@types/node": "^7.0.29", + "@types/semver": "^5.3.33", "commitizen": "^2.9.6", "cz-conventional-changelog": "^2.0.0", "danger": "*", diff --git a/yarn.lock b/yarn.lock index 04e2304..862b84d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -74,6 +74,10 @@ version "7.0.29" resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.29.tgz#ccfcec5b7135c7caf6c4ffb8c7f33102340d99df" +"@types/semver@^5.3.33": + version "5.3.33" + resolved "https://registry.yarnpkg.com/@types/semver/-/semver-5.3.33.tgz#58ebb6c8c48e161e24f8901915e7184900d341f7" + abab@^1.0.0, abab@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/abab/-/abab-1.0.3.tgz#b81de5f7274ec4e756d797cd834f303642724e5d" From caad0fd8bf27967a76d0de0c309f9a2fe6edb336 Mon Sep 17 00:00:00 2001 From: Macklin Underdown Date: Thu, 17 Aug 2017 11:47:51 -0400 Subject: [PATCH 2/9] fix(license): check versions when npm.license is missing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-by: Elan Ruusamäe --- src/index.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index a297877..f085855 100644 --- a/src/index.ts +++ b/src/index.ts @@ -210,9 +210,14 @@ export const getNPMMetadataForDep = async ( tableDeets.push({ name: "License", message: license }) } else { // License is important, so always show info + const { versions = {} } = npm + const licenses = Object.keys(versions) + .sort((a, b) => (semver.gte(b, a) ? 1 : 0)) // sort latest versions first + .map(version => versions[version].license) // get the license + .filter(Boolean) // remove falsy values tableDeets.push({ name: "License", - message: "NO LICENSE FOUND", + message: `${licenses[0] || "NO LICENSE FOUND"}`, }) } // Right From b5d95e599701632be3adac557e836030585549f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 22 Mar 2022 12:05:02 +0200 Subject: [PATCH 3/9] Add helper to provide fixtures to node-fetch --- src/index.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/index.test.ts b/src/index.test.ts index 8c5d12a..632ab01 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -15,6 +15,13 @@ import yarn, { getNPMMetadataForDep, } from "./index" +const provideFixture = (fixture: string) => { + return () => () => Promise.resolve({ + ok: true, + json: () => Promise.resolve(JSON.parse(mockfs.readFileSync(`src/fixtures/${fixture}.json`, "utf8"))), + }) +}; + declare const global: any beforeEach(() => { global.warn = jest.fn() From d54cde89471c5157bebe63b6a55b740e45a7d830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 22 Mar 2022 12:05:13 +0200 Subject: [PATCH 4/9] Use specific node-fetch fixtures --- src/index.test.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index 632ab01..d91be12 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,10 +1,4 @@ import * as mockfs from "fs" -jest.mock("node-fetch", () => () => - Promise.resolve({ - ok: true, - json: () => Promise.resolve(JSON.parse(mockfs.readFileSync("src/fixtures/danger-npm-info.json", "utf8"))), - }) -) import yarn, { _operateOnSingleDiff, @@ -22,6 +16,8 @@ const provideFixture = (fixture: string) => { }) }; +const fixtureDangerNpmInfo = provideFixture("danger-npm-info"); + declare const global: any beforeEach(() => { global.warn = jest.fn() @@ -129,6 +125,7 @@ describe("checkForLockfileDiff", () => { describe("npm metadata", () => { it("Shows a bunch of useful text for a new dep", async () => { + jest.mock("node-fetch", () => () => fixtureDangerNpmInfo) expect.assertions(1) const npmData = await getNPMMetadataForDep("danger") expect(_renderNPMTable({ usedInPackageJSONPaths: ["package.json"], npmData: npmData! })).toMatchSnapshot() From 92ce56acdeb4de46943f42ef8fc0ac0f1bff5c03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 22 Mar 2022 12:08:14 +0200 Subject: [PATCH 5/9] Add fixture for pinpoint-npm-info Co-Authored-by: Macklin Underdown --- src/fixtures/pinpoint-npm-info.json | 135 ++++++++++++++++++++++++++++ src/index.test.ts | 1 + 2 files changed, 136 insertions(+) create mode 100644 src/fixtures/pinpoint-npm-info.json diff --git a/src/fixtures/pinpoint-npm-info.json b/src/fixtures/pinpoint-npm-info.json new file mode 100644 index 0000000..e4d1743 --- /dev/null +++ b/src/fixtures/pinpoint-npm-info.json @@ -0,0 +1,135 @@ +{ + "_attachments": {}, + "_id": "pinpoint", + "_rev": "5-25a718426e3c3e3763b7a8f85b9fe65b", + "author": { + "email": "curvedmark@gmail.com", + "name": "Glen Huang" + }, + "description": "Display an arrow in a string of code to point to a location", + "dist-tags": { + "latest": "1.1.0" + }, + "maintainers": [ + { + "email": "curvedmark@gmail.com", + "name": "curvedmark" + } + ], + "name": "pinpoint", + "readme": "# pinpoint\n\nAdd line numbers and an arrow to a string of code that points to a specific location.\n\n## Installation\n\n\tnpm install pinpoint\n\n## Example\n\nWith this code:\n\n```javascript\nfunction add(left, right) {\n return left + right;\n}\n\nconsole.log(add(1 + 1));\n```\n\nWhen being asked to point to line 2 and column 2, generates this:\n\n```javascript\n1| function add(left, right) {\n2| return left + right;\n-----^\n3| }\n4|\n5| console.log(add(1 + 1));\n```\n## API\n\n```javascript\nvar pinpoint = require('pinpoint');\n\nvar str = pinpoint(code, options);\n```\n\n* `code` - a string of code\n* `options` - an object literal supports these options:\n\t* `line` - the line number to point to, 1-based.\n\t* `column` - the column number to point to, 1-based.\n\t* `showLines` - the number of lines to show. The pointed line will try to stay in the middle of the displays lines.\n\t* `indent` - indent string prepend to each line, before the line number.", + "repository": { + "type": "git", + "url": "git://github.com/curvedmark/pinpoint.git" + }, + "time": { + "1.0.0": "2013-08-19T09:30:51.336Z", + "1.1.0": "2013-09-21T09:56:46.195Z", + "created": "2013-08-19T09:30:46.479Z", + "modified": "2013-09-21T09:56:46.195Z" + }, + "versions": { + "1.0.0": { + "_from": ".", + "_id": "pinpoint@1.0.0", + "_npmUser": { + "email": "curvedmark@gmail.com", + "name": "curvedmark" + }, + "_npmVersion": "1.3.5", + "author": { + "email": "curvedmark@gmail.com", + "name": "Glen Huang" + }, + "bugs": { + "url": "https://github.com/curvedmark/pinpoint/issues" + }, + "description": "Display an arrow in a string of code to point to a location", + "devDependencies": { + "coffee-script": "1.x", + "mocha": "1.x" + }, + "directories": { + "lib": "lib", + "test": "test" + }, + "dist": { + "shasum": "11469494e0b14816e2fbebdd88ae189ac1a97288", + "tarball": "https://registry.npmjs.org/pinpoint/-/pinpoint-1.0.0.tgz" + }, + "keywords": [ + "editor" + ], + "license": "MIT", + "main": "lib/pinpoint.js", + "maintainers": [ + { + "email": "curvedmark@gmail.com", + "name": "curvedmark" + } + ], + "name": "pinpoint", + "readme": "# pinpoint\n\nAdd line numbers and an arrow to a string of code that points to a specific location.\n\n## Installation\n\n\tnpm install pinpoint\n\n## Example\n\nWith this code:\n\n```javascript\nfunction add(left, right) {\n return left + right;\n}\n\nconsole.log(add(1 + 1));\n```\n\nWhen being asked to point to line 2 and column 2, generates this:\n\n```javascript\n1| function add(left, right) {\n2| return left + right;\n-----^\n3| }\n4|\n5| console.log(add(1 + 1));\n```\n## API\n\n```javascript\nvar pinpoint = require('pinpoint');\n\nvar str = pinpoint(code, options);\n```\n\n* `code` - a string of code\n* `options` - an object literal supports these options:\n\t* `line` - the line number to point to, 1-based.\n\t* `column` - the column number to point to, 1-based.\n\t* `showLines` - the number of lines to show. The pointed line will try to stay in the middle of the displays lines.\n\t* `indent` - indent string prepend to each line, before the line number.", + "readmeFilename": "README.md", + "repository": { + "type": "git", + "url": "git://github.com/curvedmark/pinpoint.git" + }, + "scripts": { + "test": "mocha --bail --compilers coffee:coffee-script --ui qunit" + }, + "version": "1.0.0" + }, + "1.1.0": { + "_from": ".", + "_id": "pinpoint@1.1.0", + "_npmUser": { + "email": "curvedmark@gmail.com", + "name": "curvedmark" + }, + "_npmVersion": "1.3.8", + "author": { + "email": "curvedmark@gmail.com", + "name": "Glen Huang" + }, + "bugs": { + "url": "https://github.com/curvedmark/pinpoint/issues" + }, + "description": "Display an arrow in a string of code to point to a location", + "devDependencies": { + "coffee-script": "1.x", + "mocha": "1.x" + }, + "directories": { + "lib": "lib", + "test": "test" + }, + "dist": { + "shasum": "0cf7757a6977f1bf7f6a32207b709e377388e874", + "tarball": "https://registry.npmjs.org/pinpoint/-/pinpoint-1.1.0.tgz" + }, + "keywords": [ + "editor" + ], + "license": "MIT", + "main": "lib/pinpoint.js", + "maintainers": [ + { + "email": "curvedmark@gmail.com", + "name": "curvedmark" + } + ], + "name": "pinpoint", + "readme": "# pinpoint\n\nAdd line numbers and an arrow to a string of code that points to a specific location.\n\n## Installation\n\n\tnpm install pinpoint\n\n## Example\n\nWith this code:\n\n```javascript\nfunction add(left, right) {\n return left + right;\n}\n\nconsole.log(add(1 + 1));\n```\n\nWhen being asked to point to line 2 and column 2, generates this:\n\n```javascript\n1| function add(left, right) {\n2| return left + right;\n-----^\n3| }\n4|\n5| console.log(add(1 + 1));\n```\n## API\n\n```javascript\nvar pinpoint = require('pinpoint');\n\nvar str = pinpoint(code, options);\n```\n\n* `code` - a string of code\n* `options` - an object literal supports these options:\n\t* `line` - the line number to point to, 1-based.\n\t* `column` - the column number to point to, 1-based.\n\t* `showLines` - the number of lines to show. The pointed line will try to stay in the middle of the displays lines.\n\t* `indent` - indent string prepend to each line, before the line number.\n\t* `tabSize` - width of a tab character.", + "readmeFilename": "README.md", + "repository": { + "type": "git", + "url": "git://github.com/curvedmark/pinpoint.git" + }, + "scripts": { + "test": "mocha --bail --compilers coffee:coffee-script --ui qunit" + }, + "version": "1.1.0" + } + } +} diff --git a/src/index.test.ts b/src/index.test.ts index d91be12..9691bd5 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -17,6 +17,7 @@ const provideFixture = (fixture: string) => { }; const fixtureDangerNpmInfo = provideFixture("danger-npm-info"); +const fixturePinpointNpmInfo = provideFixture("pinpoint-npm-info"); declare const global: any beforeEach(() => { From e888145c7e346c13e3687c0205cee954e46d6156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 22 Mar 2022 11:57:26 +0200 Subject: [PATCH 6/9] Update tests for license tests Co-Authored-by: Macklin Underdown --- src/index.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/index.test.ts b/src/index.test.ts index 9691bd5..6bd07ec 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -4,9 +4,9 @@ import yarn, { _operateOnSingleDiff, _renderNPMTable, checkForLockfileDiff, + checkForNewDependencies, checkForRelease, checkForTypesInDeps, - getNPMMetadataForDep, } from "./index" const provideFixture = (fixture: string) => { @@ -193,5 +193,11 @@ describe("Feature Flags", () => { expect(global.message).toHaveBeenCalledTimes(0) expect(global.warn).toHaveBeenCalledTimes(2) // Called with "Changes were made to package.json, but not […]" expect(global.fail).toHaveBeenCalledTimes(0) + + it("looks through versions if license is missing", async () => { + jest.mock("node-fetch", fixturePinpointNpmInfo) + const { getNPMMetadataForDep } = require("./") + const data = await getNPMMetadataForDep("pinpoint") + expect(data).toMatchSnapshot() }) }) From cb86de7270996045cbd1dc9e0eae88ddd1ae3f71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 22 Mar 2022 12:24:02 +0200 Subject: [PATCH 7/9] fixup! Use specific node-fetch fixtures --- src/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.test.ts b/src/index.test.ts index 6bd07ec..fcfff5f 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -126,7 +126,7 @@ describe("checkForLockfileDiff", () => { describe("npm metadata", () => { it("Shows a bunch of useful text for a new dep", async () => { - jest.mock("node-fetch", () => () => fixtureDangerNpmInfo) + jest.mock("node-fetch", fixtureDangerNpmInfo) expect.assertions(1) const npmData = await getNPMMetadataForDep("danger") expect(_renderNPMTable({ usedInPackageJSONPaths: ["package.json"], npmData: npmData! })).toMatchSnapshot() From 1d9e447075f7432725776d3c08b08a96504c61e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 22 Mar 2022 12:24:49 +0200 Subject: [PATCH 8/9] fixup! Update tests for license tests --- src/index.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.test.ts b/src/index.test.ts index fcfff5f..c2967d7 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -7,6 +7,7 @@ import yarn, { checkForNewDependencies, checkForRelease, checkForTypesInDeps, + getNPMMetadataForDep, } from "./index" const provideFixture = (fixture: string) => { From 58d8b951165ad44df1064da7a18af43a97f5ba87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 22 Mar 2022 12:28:45 +0200 Subject: [PATCH 9/9] temp hackxxx --- src/index.test.ts | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index c2967d7..afe87fa 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -126,8 +126,32 @@ describe("checkForLockfileDiff", () => { }) describe("npm metadata", () => { + + jest.mock("node-fetch", () => () => + Promise.resolve({ + ok: true, + json: () => Promise.resolve(JSON.parse(mockfs.readFileSync("src/fixtures/danger-npm-info.json", "utf8"))), + }), + ) it("Shows a bunch of useful text for a new dep", async () => { - jest.mock("node-fetch", fixtureDangerNpmInfo) + + // const provideFixture2 = (fixture: string) => { + // return () => () => Promise.resolve({ + // ok: true, + // json: () => Promise.resolve(JSON.parse(mockfs.readFileSync(`src/fixtures/${fixture}.json`, "utf8"))), + // }) + // }; + // + // const fixtureDangerNpmInfo2 = provideFixture2("danger-npm-info"); + // jest.mock("node-fetch", fixtureDangerNpmInfo2) + + jest.mock("node-fetch", () => () => + Promise.resolve({ + ok: true, + json: () => Promise.resolve(JSON.parse(mockfs.readFileSync("src/fixtures/danger-npm-info.json", "utf8"))), + }), + ) + expect.assertions(1) const npmData = await getNPMMetadataForDep("danger") expect(_renderNPMTable({ usedInPackageJSONPaths: ["package.json"], npmData: npmData! })).toMatchSnapshot() @@ -194,11 +218,11 @@ describe("Feature Flags", () => { expect(global.message).toHaveBeenCalledTimes(0) expect(global.warn).toHaveBeenCalledTimes(2) // Called with "Changes were made to package.json, but not […]" expect(global.fail).toHaveBeenCalledTimes(0) - - it("looks through versions if license is missing", async () => { - jest.mock("node-fetch", fixturePinpointNpmInfo) - const { getNPMMetadataForDep } = require("./") - const data = await getNPMMetadataForDep("pinpoint") - expect(data).toMatchSnapshot() - }) + // + // it("looks through versions if license is missing", async () => { + // // jest.mock("node-fetch", fixturePinpointNpmInfo) + // const { getNPMMetadataForDep } = require("./") + // const data = await getNPMMetadataForDep("pinpoint") + // expect(data).toMatchSnapshot() + // }) })