sourcecred/src/cli/common.test.js
William Chargin d685ebbdd4
cli: add a common module for environment vars (#742)
Summary:
This includes environment variables to specify the SourceCred directory
and the GitHub token. Parts of this may change once #638 is resolved.

Test Plan:
Unit tests included, with full coverage; run `yarn unit`.

wchargin-branch: cli-common
2018-09-02 16:03:38 -07:00

58 lines
1.6 KiB
JavaScript

// @flow
import path from "path";
import {
defaultPlugins,
defaultSourcecredDirectory,
sourcecredDirectory,
githubToken,
} from "./common";
describe("cli/common", () => {
beforeEach(() => {
jest
.spyOn(require("os"), "tmpdir")
.mockReturnValue(path.join("/", "your", "tmpdir"));
});
describe("defaultPlugins", () => {
it("gives an array including the Git plugin name", () => {
expect(defaultPlugins()).toEqual(expect.arrayContaining(["git"]));
});
});
describe("defaultSourcecredDirectory", () => {
it("gives a file under the OS's temporary directory", () => {
expect(defaultSourcecredDirectory()).toEqual(
path.join("/", "your", "tmpdir", "sourcecred")
);
});
});
describe("sourcecredDirectory", () => {
it("uses the environment variable when available", () => {
const dir = path.join("/", "my", "sourcecred");
process.env.SOURCECRED_DIRECTORY = dir;
expect(sourcecredDirectory()).toEqual(dir);
});
it("uses the default directory if no environment variable is set", () => {
delete process.env.SOURCECRED_DIRECTORY;
expect(sourcecredDirectory()).toEqual(
path.join("/", "your", "tmpdir", "sourcecred")
);
});
});
describe("githubToken", () => {
it("uses the environment variable when available", () => {
process.env.SOURCECRED_GITHUB_TOKEN = "010101";
expect(githubToken()).toEqual("010101");
});
it("returns `null` if the environment variable is not set", () => {
delete process.env.SOURCECRED_GITHUB_TOKEN;
expect(githubToken()).toBe(null);
});
});
});