|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { mkdtemp, mkdir, readFile, rm } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { fileURLToPath, pathToFileURL } from "node:url"; |
| 6 | + |
| 7 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 8 | + |
| 9 | +import { |
| 10 | + azureFeedLocalRegistry, |
| 11 | + buildProjectNpmConfig, |
| 12 | + cfsRegistry, |
| 13 | + credentialProviderRegistry, |
| 14 | + getAuthCommands, |
| 15 | + getCommandInvocation, |
| 16 | + getProjectNpmrcPaths, |
| 17 | + main, |
| 18 | + refreshNpmAuthentication, |
| 19 | + runCommand, |
| 20 | + writeProjectNpmConfigs, |
| 21 | +} from "../../scripts/npm-auth-refresh.mjs"; |
| 22 | + |
| 23 | +const scriptPath = fileURLToPath(new URL("../../scripts/npm-auth-refresh.mjs", import.meta.url)); |
| 24 | +const temporaryDirectories: string[] = []; |
| 25 | + |
| 26 | +async function createTemporaryNpmrcPaths(): Promise<string[]> { |
| 27 | + const repositoryRoot = await mkdtemp(path.join(tmpdir(), "copilot-sdk-npm-auth-")); |
| 28 | + temporaryDirectories.push(repositoryRoot); |
| 29 | + |
| 30 | + const directories = [ |
| 31 | + path.join(repositoryRoot, "nodejs"), |
| 32 | + path.join(repositoryRoot, "test", "harness"), |
| 33 | + path.join(repositoryRoot, "java", "scripts", "codegen"), |
| 34 | + ]; |
| 35 | + await Promise.all(directories.map((directory) => mkdir(directory, { recursive: true }))); |
| 36 | + return directories.map((directory) => path.join(directory, ".npmrc")); |
| 37 | +} |
| 38 | + |
| 39 | +afterEach(async () => { |
| 40 | + await Promise.all( |
| 41 | + temporaryDirectories.splice(0).map((directory) => rm(directory, { recursive: true })) |
| 42 | + ); |
| 43 | +}); |
| 44 | + |
| 45 | +describe("local npm authentication refresh", () => { |
| 46 | + it.each(["--help", "-h"])("prints help successfully for %s", (flag) => { |
| 47 | + const result = spawnSync(process.execPath, [scriptPath, flag], { |
| 48 | + encoding: "utf8", |
| 49 | + }); |
| 50 | + |
| 51 | + expect(result.status).toBe(0); |
| 52 | + expect(result.stdout).toContain("Usage: npm run auth:refresh"); |
| 53 | + }); |
| 54 | + |
| 55 | + it.each([[[]], [["--refresh"]], [["--run", "unexpected"]]])( |
| 56 | + "requires the explicit --run argument for %j", |
| 57 | + (args) => { |
| 58 | + const result = spawnSync(process.execPath, [scriptPath, ...args], { |
| 59 | + encoding: "utf8", |
| 60 | + }); |
| 61 | + |
| 62 | + expect(result.status).toBe(1); |
| 63 | + expect(result.stdout).toContain("Usage: npm run auth:refresh"); |
| 64 | + } |
| 65 | + ); |
| 66 | + |
| 67 | + it("runs authentication only for --run", () => { |
| 68 | + const refresh = vi.fn(); |
| 69 | + |
| 70 | + expect(main(["--run"], refresh)).toBe(0); |
| 71 | + expect(refresh).toHaveBeenCalledOnce(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("resolves all project configs from the script URL", () => { |
| 75 | + const repositoryRoot = path.resolve(path.dirname(scriptPath), ".."); |
| 76 | + expect(getProjectNpmrcPaths(pathToFileURL(scriptPath).href)).toEqual([ |
| 77 | + path.join(repositoryRoot, "nodejs", ".npmrc"), |
| 78 | + path.join(repositoryRoot, "test", "harness", ".npmrc"), |
| 79 | + path.join(repositoryRoot, "java", "scripts", "codegen", ".npmrc"), |
| 80 | + ]); |
| 81 | + }); |
| 82 | + |
| 83 | + it("writes only the scoped registry to all three project configs", async () => { |
| 84 | + const npmrcPaths = await createTemporaryNpmrcPaths(); |
| 85 | + |
| 86 | + writeProjectNpmConfigs(npmrcPaths); |
| 87 | + |
| 88 | + const expected = `@github:registry=${azureFeedLocalRegistry}\n`; |
| 89 | + await Promise.all( |
| 90 | + npmrcPaths.map(async (npmrcPath) => { |
| 91 | + await expect(readFile(npmrcPath, "utf8")).resolves.toBe(expected); |
| 92 | + }) |
| 93 | + ); |
| 94 | + expect(buildProjectNpmConfig()).not.toMatch(/^registry=/m); |
| 95 | + expect(buildProjectNpmConfig()).not.toMatch(/(?:_auth|token|password)/i); |
| 96 | + }); |
| 97 | + |
| 98 | + it("authenticates once using the nodejs config", () => { |
| 99 | + const npmrcPaths = [ |
| 100 | + "C:\\repo\\nodejs\\.npmrc", |
| 101 | + "C:\\repo\\test\\harness\\.npmrc", |
| 102 | + "C:\\repo\\java\\scripts\\codegen\\.npmrc", |
| 103 | + ]; |
| 104 | + const writer = vi.fn(); |
| 105 | + const runner = vi.fn(); |
| 106 | + |
| 107 | + refreshNpmAuthentication("win32", npmrcPaths, writer, runner); |
| 108 | + |
| 109 | + expect(writer).toHaveBeenCalledOnce(); |
| 110 | + expect(writer).toHaveBeenCalledWith(npmrcPaths); |
| 111 | + expect(runner).toHaveBeenCalledTimes(2); |
| 112 | + expect(runner).toHaveBeenLastCalledWith( |
| 113 | + "vsts-npm-auth.cmd", |
| 114 | + ["-config", npmrcPaths[0], "-Force", "-ReadOnly"], |
| 115 | + "win32" |
| 116 | + ); |
| 117 | + }); |
| 118 | + |
| 119 | + it("uses vsts-npm-auth on Windows", () => { |
| 120 | + expect(getAuthCommands("win32", "C:\\repo\\nodejs\\.npmrc")).toEqual([ |
| 121 | + { |
| 122 | + command: "npm.cmd", |
| 123 | + args: ["install", "--global", "vsts-npm-auth@0.43.0", `--registry=${cfsRegistry}`], |
| 124 | + }, |
| 125 | + { |
| 126 | + command: "vsts-npm-auth.cmd", |
| 127 | + args: ["-config", "C:\\repo\\nodejs\\.npmrc", "-Force", "-ReadOnly"], |
| 128 | + }, |
| 129 | + ]); |
| 130 | + }); |
| 131 | + |
| 132 | + it("launches Windows command shims through the command interpreter", () => { |
| 133 | + expect( |
| 134 | + getCommandInvocation( |
| 135 | + "win32", |
| 136 | + "npm.cmd", |
| 137 | + ["--version"], |
| 138 | + "C:\\Windows\\System32\\cmd.exe" |
| 139 | + ) |
| 140 | + ).toEqual({ |
| 141 | + command: "C:\\Windows\\System32\\cmd.exe", |
| 142 | + args: ["/d", "/s", "/c", "npm.cmd", "--version"], |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + it("surfaces command spawn errors", () => { |
| 147 | + expect(() => |
| 148 | + runCommand(path.join(tmpdir(), "copilot-sdk-command-does-not-exist"), [], "linux") |
| 149 | + ).toThrow(); |
| 150 | + }); |
| 151 | + |
| 152 | + it("surfaces nonzero command exit statuses", () => { |
| 153 | + expect(() => runCommand(process.execPath, ["-e", "process.exit(7)"], "linux")).toThrow( |
| 154 | + "exited with code 7" |
| 155 | + ); |
| 156 | + }); |
| 157 | + |
| 158 | + it.each(["linux", "darwin"])("uses the Azure credential provider on %s", (platform) => { |
| 159 | + expect(getAuthCommands(platform, "/repo/nodejs/.npmrc")).toEqual([ |
| 160 | + { |
| 161 | + command: "npm", |
| 162 | + args: [ |
| 163 | + "install", |
| 164 | + "--global", |
| 165 | + "@microsoft/artifacts-npm-credprovider@1.1.3", |
| 166 | + `--registry=${credentialProviderRegistry}`, |
| 167 | + `--@microsoft:registry=${credentialProviderRegistry}`, |
| 168 | + ], |
| 169 | + }, |
| 170 | + { |
| 171 | + command: "artifacts-npm-credprovider", |
| 172 | + args: ["-c", "/repo/nodejs/.npmrc"], |
| 173 | + }, |
| 174 | + ]); |
| 175 | + expect(getCommandInvocation(platform, "npm", ["--version"])).toEqual({ |
| 176 | + command: "npm", |
| 177 | + args: ["--version"], |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments