Skip to content

Commit

Permalink
fix: npm auth null (#81)
Browse files Browse the repository at this point in the history
An error was introduced a while ago where npm-auth loaded from .upmconfig would be discarded and null always returned. This was fixed. Also added a few tests to test for this behaviour-
  • Loading branch information
ComradeVanti committed Dec 28, 2023
1 parent 7f6b331 commit 1ac0c6e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const parseEnv = async function (
`failed to parse auth info for ${registry} in .upmconfig.toml: missing token or _auth fields`
);
}
return null;
return npmAuth;
}

if (upmConfig !== undefined && upmConfig.npmAuth !== undefined) {
Expand Down
6 changes: 5 additions & 1 deletion src/utils/upm-config-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ export const loadUpmConfig = async (
* @param configDir The directory in which to save the config
*/
export const saveUpmConfig = async (config: UPMConfig, configDir: string) => {
mkdirp.sync(configDir);
try {
mkdirp.sync(configDir);
} catch {
/* empty */
}
const configPath = path.join(configDir, configFileName);
const content = TOML.stringify(config);
fs.writeFileSync(configPath, content, "utf8");
Expand Down
52 changes: 52 additions & 0 deletions test/test-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@ import { parseEnv } from "../src/utils/env";
import { createWorkDir, getWorkDir, removeWorkDir } from "./mock-work-dir";
import { attachMockConsole, MockConsole } from "./mock-console";
import should from "should";
import { runWithEnv } from "./mock-env";
import { saveUpmConfig } from "../src/utils/upm-config-io";
import { registryUrl } from "../src/types/registry-url";
import { TokenAuth, UPMConfig } from "../src/types/upm-config";
import { NpmAuth } from "another-npm-registry-client";

const testUpmAuth: TokenAuth = {
email: "test@mail.com",
token: "ThisIsNotAValidToken",
};

const testNpmAuth: NpmAuth = {
token: "ThisIsNotAValidToken",
alwaysAuth: false,
};

const testUpmConfig: UPMConfig = {
npmAuth: { [registryUrl("http://registry.npmjs.org")]: testUpmAuth },
};

describe("env", function () {
describe("parseEnv", function () {
Expand All @@ -18,6 +37,7 @@ describe("env", function () {
manifest: false,
editorVersion: " 2019.2.13f1",
});
saveUpmConfig(testUpmConfig, getWorkDir("test-openupm-cli"));
});
after(function () {
removeWorkDir("test-openupm-cli");
Expand Down Expand Up @@ -132,6 +152,38 @@ describe("env", function () {
should(env).not.be.null();
env!.registry.url.should.be.equal("http://[1:2:3:4:5:6:7:8]:4873");
});
it("should have registry auth if specified", async function () {
const projectDir = getWorkDir("test-openupm-cli");
const env = await runWithEnv({ HOME: projectDir }, () =>
parseEnv(
{
_global: {
registry: "registry.npmjs.org",
chdir: projectDir,
},
},
true
)
);
should(env).not.be.null();
should(env!.registry.auth).deepEqual(testNpmAuth);
});
it("should not have unspecified registry auth", async function () {
const projectDir = getWorkDir("test-openupm-cli");
const env = await runWithEnv({ HOME: projectDir }, () =>
parseEnv(
{
_global: {
registry: "registry.other.org",
chdir: projectDir,
},
},
true
)
);
should(env).not.be.null();
should(env!.registry.auth).be.null();
});
it("upstream", async function () {
const env = await parseEnv({ _global: { upstream: false } }, false);
should(env).not.be.null();
Expand Down

0 comments on commit 1ac0c6e

Please sign in to comment.