|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import * as ini from "ini"; |
| 4 | +import fixtures from "fixturez"; |
| 5 | +import { prepareNpmConfig } from "./npmUtils"; |
| 6 | + |
| 7 | +const f = fixtures(__dirname); |
| 8 | +const authToken = "npm_abc"; |
| 9 | + |
| 10 | +describe("prepareNpmConfig", () => { |
| 11 | + let tmpDir: string; |
| 12 | + let npmrcPath: string; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + tmpDir = f.temp(); |
| 16 | + npmrcPath = path.join(tmpDir, ".npmrc"); |
| 17 | + }); |
| 18 | + |
| 19 | + describe("when .npmrc exists", () => { |
| 20 | + describe("when authToken is defined", () => { |
| 21 | + beforeEach(() => { |
| 22 | + fs.writeFileSync( |
| 23 | + npmrcPath, |
| 24 | + ini.stringify({ |
| 25 | + email: "npm@company.com", |
| 26 | + "//registry.npmjs.org/:_authToken": authToken, |
| 27 | + }) |
| 28 | + ); |
| 29 | + }); |
| 30 | + test("should not change the .npmrc file", () => { |
| 31 | + prepareNpmConfig({ HOME: tmpDir }); |
| 32 | + |
| 33 | + const npmConfig = ini.parse(fs.readFileSync(npmrcPath, "utf-8")); |
| 34 | + expect(npmConfig).toMatchInlineSnapshot(` |
| 35 | + Object { |
| 36 | + "//registry.npmjs.org/:_authToken": "npm_abc", |
| 37 | + "email": "npm@company.com", |
| 38 | + } |
| 39 | + `); |
| 40 | + }); |
| 41 | + }); |
| 42 | + describe("when authToken is not defined", () => { |
| 43 | + beforeEach(() => { |
| 44 | + fs.writeFileSync( |
| 45 | + npmrcPath, |
| 46 | + ini.stringify({ |
| 47 | + email: "npm@company.com", |
| 48 | + }) |
| 49 | + ); |
| 50 | + }); |
| 51 | + describe("when NPM_TOKEN environment variable is not defined", () => { |
| 52 | + test("it should throw an error", () => { |
| 53 | + expect(() => |
| 54 | + prepareNpmConfig({ |
| 55 | + HOME: tmpDir, |
| 56 | + NPM_TOKEN: undefined, |
| 57 | + }) |
| 58 | + ).toThrowErrorMatchingInlineSnapshot( |
| 59 | + `"Missing NPM authToken. Please make sure you have the \`NPM_TOKEN\` environment variable defined."` |
| 60 | + ); |
| 61 | + }); |
| 62 | + }); |
| 63 | + test("should inject NPM_TOKEN value in .npmrc file", () => { |
| 64 | + prepareNpmConfig({ |
| 65 | + HOME: tmpDir, |
| 66 | + NPM_TOKEN: authToken, |
| 67 | + }); |
| 68 | + |
| 69 | + const npmConfig = ini.parse(fs.readFileSync(npmrcPath, "utf-8")); |
| 70 | + expect(npmConfig).toMatchInlineSnapshot(` |
| 71 | + Object { |
| 72 | + "//registry.npmjs.org/:_authToken": "npm_abc", |
| 73 | + "email": "npm@company.com", |
| 74 | + } |
| 75 | + `); |
| 76 | + }); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe("when .npmrc does not exist", () => { |
| 81 | + describe("when NPM_TOKEN environment variable is not defined", () => { |
| 82 | + test("it should throw an error", () => { |
| 83 | + expect(() => |
| 84 | + prepareNpmConfig({ |
| 85 | + HOME: tmpDir, |
| 86 | + NPM_TOKEN: undefined, |
| 87 | + }) |
| 88 | + ).toThrowErrorMatchingInlineSnapshot( |
| 89 | + `"Missing NPM authToken. Please make sure you have the \`NPM_TOKEN\` environment variable defined."` |
| 90 | + ); |
| 91 | + }); |
| 92 | + }); |
| 93 | + test("should create a new .npmrc config", () => { |
| 94 | + prepareNpmConfig({ |
| 95 | + HOME: tmpDir, |
| 96 | + NPM_TOKEN: authToken, |
| 97 | + }); |
| 98 | + |
| 99 | + const npmConfig = ini.parse(fs.readFileSync(npmrcPath, "utf-8")); |
| 100 | + expect(npmConfig).toMatchInlineSnapshot(` |
| 101 | + Object { |
| 102 | + "//registry.npmjs.org/:_authToken": "npm_abc", |
| 103 | + } |
| 104 | + `); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments