Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/src/Program.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ class Program {
.description(this.description)
.parse(props.argv);

// Commander keeps the parsed options on opts(). Reading one off the command itself gives
// undefined without complaining, which here meant --config was ignored and the changelog
// was built from whichever .grenrc happened to be in the directory.
const opts = this.program.opts();

this.options = Object.assign(
{},
getGrenConfig(props.cwd, program.config),
this._getOptionsFromObject(this.program.opts(), this.defaults),
getGrenConfig(props.cwd, opts.config),
this._getOptionsFromObject(opts, this.defaults),
);
}

Expand Down
47 changes: 47 additions & 0 deletions test/Program.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { assert } from "chai";
import Program from "../lib/src/Program.js";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

describe("Program", () => {
const testCommand = new Program({
Expand Down Expand Up @@ -164,6 +167,50 @@ describe("Program", () => {
});
});

describe("config", () => {
const options = [
{ short: "-c", name: "config", valueType: "<string>", description: "Custom config" },
];
let cwd;

before(() => {
cwd = fs.mkdtempSync(path.join(os.tmpdir(), "gren-config-"));
fs.writeFileSync(path.join(cwd, ".grenrc"), JSON.stringify({ repo: "from-the-default" }));
fs.writeFileSync(path.join(cwd, "custom.json"), JSON.stringify({ repo: "from-the-custom" }));
});

after(() => {
fs.rmSync(cwd, { recursive: true, force: true });
});

const program = (argv) =>
new Program({ name: "Test", description: "This is a test", options, argv, cwd });

it("Should read the file --config names", () => {
assert.equal(
program(["node", "gren", "--config", "custom.json"]).options.repo,
"from-the-custom",
);
});

it("Should read the file -c names", () => {
assert.equal(program(["node", "gren", "-c", "custom.json"]).options.repo, "from-the-custom");
});

it("Should fall back to the conventional filenames", () => {
assert.equal(program(["node", "gren"]).options.repo, "from-the-default");
});

it("Should stop when --config names a file that is not there", () => {
// Silently using another file would build the changelog from a configuration the user
// did not ask for, and the output would look like a bug in gren.
assert.throws(
() => program(["node", "gren", "--config", "not-there.json"]),
/Could not find custom config file/,
);
});
});

describe("_filterObject", () => {
it("Should return an object without undefined values", () => {
assert.deepEqual(
Expand Down
Loading