Skip to content

Commit

Permalink
refactor: remove check-path
Browse files Browse the repository at this point in the history
The `parseEnv` function had the option to check some of the file-system dependent checks. This was only used by tests. After removing this logic it seemed that only the specific tests which concerned themselves with the check-path logic failed.

Remove the check-path logic and relevant tests. To make sure this does not break anything a following commit will make the env tests more rigorous.
  • Loading branch information
ComradeVanti committed Mar 20, 2024
1 parent ea8594a commit 843bea1
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 105 deletions.
2 changes: 1 addition & 1 deletion src/cmd-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const add = async function (
): Promise<Result<void, AddError[]>> {
if (!Array.isArray(pkgs)) pkgs = [pkgs];
// parse env
const envResult = await parseEnv(options, true);
const envResult = await parseEnv(options);
if (envResult.isErr()) return Err([envResult.error]);
const env = envResult.value;

Expand Down
2 changes: 1 addition & 1 deletion src/cmd-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const deps = async function (
options: DepsOptions
): Promise<Result<void, DepsError>> {
// parse env
const envResult = await parseEnv(options, true);
const envResult = await parseEnv(options);
if (envResult.isErr()) return envResult;
const env = envResult.value;

Expand Down
2 changes: 1 addition & 1 deletion src/cmd-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const login = async function (
options: LoginOptions
): Promise<Result<void, LoginError>> {
// parse env
const envResult = await parseEnv(options, true);
const envResult = await parseEnv(options);
if (envResult.isErr()) return envResult;
const env = envResult.value;

Expand Down
2 changes: 1 addition & 1 deletion src/cmd-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const remove = async function (
): Promise<Result<void, RemoveError[]>> {
if (!Array.isArray(pkgs)) pkgs = [pkgs];
// parse env
const envResult = await parseEnv(options, true);
const envResult = await parseEnv(options);
if (envResult.isErr()) return Err([envResult.error]);
const env = envResult.value;

Expand Down
2 changes: 1 addition & 1 deletion src/cmd-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function search(
options: SearchOptions
): Promise<Result<void, SearchError>> {
// parse env
const envResult = await parseEnv(options, true);
const envResult = await parseEnv(options);
if (envResult.isErr()) return envResult;
const env = envResult.value;

Expand Down
2 changes: 1 addition & 1 deletion src/cmd-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const view = async function (
options: ViewOptions
): Promise<Result<void, ViewError>> {
// parse env
const envResult = await parseEnv(options, true);
const envResult = await parseEnv(options);
if (envResult.isErr()) return envResult;
const env = envResult.value;

Expand Down
14 changes: 7 additions & 7 deletions src/types/project-manifest.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {DomainName} from "./domain-name";
import {SemanticVersion} from "./semantic-version";
import {PackageUrl} from "./package-url";
import {ScopedRegistry} from "./scoped-registry";
import {RegistryUrl} from "./registry-url";
import {removeTrailingSlash} from "../utils/string-utils";
import {removeRecordKey} from "../utils/record-utils";
import { DomainName } from "./domain-name";
import { SemanticVersion } from "./semantic-version";
import { PackageUrl } from "./package-url";
import { ScopedRegistry } from "./scoped-registry";
import { RegistryUrl } from "./registry-url";
import { removeTrailingSlash } from "../utils/string-utils";
import { removeRecordKey } from "../utils/record-utils";

/**
* The content of the project-manifest (manifest.json) of a Unity project.
Expand Down
15 changes: 1 addition & 14 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ function determineIsSystemUser(options: CmdOptions): boolean {
* Attempts to parse env.
*/
export const parseEnv = async function (
options: CmdOptions,
checkPath: boolean
options: CmdOptions
): Promise<Result<Env, EnvParseError>> {
// log level
log.level = determineLogLevel(options);
Expand Down Expand Up @@ -135,18 +134,6 @@ export const parseEnv = async function (
const registry = determinePrimaryRegistry(options, upmConfig);
const upstreamRegistry = determineUpstreamRegistry(options, upmConfig);

// return if no need to check path
if (!checkPath)
return Ok({
cwd: "",
editorVersion: null,
registry,
systemUser,
upstream,
upstreamRegistry,
wsl,
});

// cwd
const cwdResult = determineCwd(options);
if (cwdResult.isErr()) {
Expand Down
113 changes: 35 additions & 78 deletions test/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,10 @@ describe("env", () => {
await mockProject.restore();
});

it("defaults", async function () {
const env = (await parseEnv({ _global: {} }, false)).unwrap();
expect(env.registry.url).toEqual("https://package.openupm.com");
expect(env.upstream).toBeTruthy();
expect(env.upstreamRegistry.url).toEqual("https://packages.unity.com");
expect(env.cwd).toEqual("");
expect(env.editorVersion === null).toBeTruthy();
});

it("check path", async function () {
const env = (await parseEnv({ _global: {} }, true)).unwrap();
expect(env.cwd).toEqual(mockProject.projectPath);
});
it("can not resolve path", async function () {
const envResult = await parseEnv(
{ _global: { chdir: "/path-not-exist" } },
true
);
const envResult = await parseEnv({
_global: { chdir: "/path-not-exist" },
});
expect(envResult.isOk()).toBeFalsy();
expect(mockConsole).toHaveLineIncluding("out", "can not resolve path");
});
Expand All @@ -73,7 +59,7 @@ describe("env", () => {
const manifestPath = manifestPathFor(mockProject.projectPath);
fse.rmSync(manifestPath);

const envResult = await parseEnv({ _global: {} }, true);
const envResult = await parseEnv({ _global: {} });
expect(envResult.isOk()).toBeFalsy();
expect(mockConsole).toHaveLineIncluding(
"out",
Expand All @@ -82,138 +68,109 @@ describe("env", () => {
});
it("custom registry", async function () {
const env = (
await parseEnv(
{ _global: { registry: "https://registry.npmjs.org" } },
false
)
await parseEnv({ _global: { registry: "https://registry.npmjs.org" } })
).unwrap();

expect(env.registry.url).toEqual("https://registry.npmjs.org");
});
it("custom registry with splash", async function () {
const env = (
await parseEnv(
{ _global: { registry: "https://registry.npmjs.org/" } },
false
)
await parseEnv({ _global: { registry: "https://registry.npmjs.org/" } })
).unwrap();

expect(env.registry.url).toEqual("https://registry.npmjs.org");
});
it("custom registry with extra path", async function () {
const env = (
await parseEnv(
{
_global: {
registry: "https://registry.npmjs.org/some",
},
await parseEnv({
_global: {
registry: "https://registry.npmjs.org/some",
},
false
)
})
).unwrap();

expect(env.registry.url).toEqual("https://registry.npmjs.org/some");
});
it("custom registry with extra path and splash", async function () {
const env = (
await parseEnv(
{
_global: {
registry: "https://registry.npmjs.org/some/",
},
await parseEnv({
_global: {
registry: "https://registry.npmjs.org/some/",
},
false
)
})
).unwrap();

expect(env.registry.url).toEqual("https://registry.npmjs.org/some");
});
it("custom registry without http", async function () {
const env = (
await parseEnv({ _global: { registry: "registry.npmjs.org" } }, false)
await parseEnv({ _global: { registry: "registry.npmjs.org" } })
).unwrap();

expect(env.registry.url).toEqual("http://registry.npmjs.org");
});
it("custom registry with ipv4+port", async function () {
const env = (
await parseEnv(
{ _global: { registry: "http://127.0.0.1:4873" } },
false
)
await parseEnv({ _global: { registry: "http://127.0.0.1:4873" } })
).unwrap();

expect(env.registry.url).toEqual("http://127.0.0.1:4873");
});
it("custom registry with ipv6+port", async function () {
const env = (
await parseEnv(
{
_global: { registry: "http://[1:2:3:4:5:6:7:8]:4873" },
},
false
)
await parseEnv({
_global: { registry: "http://[1:2:3:4:5:6:7:8]:4873" },
})
).unwrap();

expect(env.registry.url).toEqual("http://[1:2:3:4:5:6:7:8]:4873");
});
it("should have registry auth if specified", async function () {
const env = (
await parseEnv(
{
_global: {
registry: "registry.npmjs.org",
},
await parseEnv({
_global: {
registry: "registry.npmjs.org",
},
true
)
})
).unwrap();

expect(env.registry.auth).toEqual(testNpmAuth);
});
it("should not have unspecified registry auth", async function () {
const env = (
await parseEnv(
{
_global: {
registry: "registry.other.org",
},
await parseEnv({
_global: {
registry: "registry.other.org",
},
true
)
})
).unwrap();

expect(env.registry.auth).toBeNull();
});
it("upstream", async function () {
const env = (
await parseEnv({ _global: { upstream: false } }, false)
).unwrap();
const env = (await parseEnv({ _global: { upstream: false } })).unwrap();

expect(env.upstream).not.toBeTruthy();
});
it("editorVersion", async function () {
const env = (await parseEnv({ _global: {} }, true)).unwrap();
const env = (await parseEnv({ _global: {} })).unwrap();

expect(env.editorVersion).toEqual("2019.2.13f1");
});
it("region cn", async function () {
const env = (await parseEnv({ _global: { cn: true } }, false)).unwrap();
const env = (await parseEnv({ _global: { cn: true } })).unwrap();

expect(env.registry.url).toEqual("https://package.openupm.cn");
expect(env.upstreamRegistry.url).toEqual("https://packages.unity.cn");
});
it("region cn with a custom registry", async function () {
const env = (
await parseEnv(
{
_global: {
cn: true,
registry: "https://reg.custom-package.com",
},
await parseEnv({
_global: {
cn: true,
registry: "https://reg.custom-package.com",
},
false
)
})
).unwrap();

expect(env.registry.url).toEqual("https://reg.custom-package.com");
Expand Down

0 comments on commit 843bea1

Please sign in to comment.