Skip to content

Commit

Permalink
feat: custom upm config path
Browse files Browse the repository at this point in the history
Currently openupm only searches for the users upm config in their home directory. But according to the docs users may override the file location by setting the UPM_USER_CONFIG_FILE env variable.

This change adds logic for using this variable as path if set.
  • Loading branch information
ComradeVanti committed Aug 13, 2024
1 parent 0651101 commit bad3c0c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/io/upm-config-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export function ResolveDefaultUpmConfigPath(
}

return async (systemUser) => {
const customDir = tryGetEnv("UPM_USER_CONFIG_FILE");
if (customDir !== null) return path.resolve(customDir);

const directory = await getConfigDirectory(systemUser);
return path.join(directory, configFileName);
};
Expand Down
28 changes: 24 additions & 4 deletions test/io/upm-config-io.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,47 @@ import {
} from "../../src/io/upm-config-io";
import { exampleRegistryUrl } from "../domain/data-registry";
import { mockService } from "../services/service.mock";
import { tryGetEnv } from "../../src/utils/env-util";

jest.mock("../../src/utils/env-util");

describe("upm-config-io", () => {
describe("resolve default path", () => {
const homeConfigPath = path.resolve("/some/home/dir/.upmconfig.toml");
const customConfigPath = path.resolve("/some/other/dir/.upmconfig.toml");

function makeDependencies() {
const getHomePath = mockService<GetHomePath>();
getHomePath.mockReturnValue(path.dirname(homeConfigPath));

jest.mocked(tryGetEnv).mockReturnValue(null);

const resolveDefaultUpmConfigPath =
ResolveDefaultUpmConfigPath(getHomePath);

return { resolveDefaultUpmConfigPath, getHomePath } as const;
}

it("should be UPM_USER_CONFIG_FILE if set", async () => {
const { resolveDefaultUpmConfigPath } = makeDependencies();
jest
.mocked(tryGetEnv)
.mockImplementation((key) =>
key === "UPM_USER_CONFIG_FILE" ? customConfigPath : null
);

const actual = await resolveDefaultUpmConfigPath(false);

expect(actual).toEqual(customConfigPath);
});

describe("no system-user", () => {
it("should be in home path", async () => {
const { resolveDefaultUpmConfigPath, getHomePath } = makeDependencies();
const expected = path.resolve("/some/home/dir/.upmconfig.toml");
getHomePath.mockReturnValue(path.dirname(expected));
const { resolveDefaultUpmConfigPath } = makeDependencies();

const actual = await resolveDefaultUpmConfigPath(false);

expect(actual).toEqual(expected);
expect(actual).toEqual(homeConfigPath);
});
});
});
Expand Down

0 comments on commit bad3c0c

Please sign in to comment.