Skip to content

Commit

Permalink
--max-file-duration bug is fixed
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
IamJeffG committed Jul 16, 2024
1 parent ef254a3 commit af78b75
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
18 changes: 17 additions & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
const { Command, Option } = require("commander");
const audiomothUtils = require("audiomoth-utils");

/**
* A base-10 version of parseInt where the second argument is a callback
* passed by Commander Option
*
* Doc: https://github.com/tj/commander.js?tab=readme-ov-file#custom-option-processing
* Motivation: https://github.com/tj/commander.js/issues/943
*
* @param {*} value
* @param {*} dummyPrevious The callback function receives two parameters: the user specified value and the
previous value for the option.
* @returns
*/
function cmdrParseInt(value, dummyPrevious) {
return parseInt(value);
}

function makeProgram() {
const program = new Command();

Expand All @@ -22,7 +38,7 @@ function makeProgram() {
"--max-file-duration [seconds]",
"max duration of expanded file to write, in seconds"
)
.argParser(parseInt)
.argParser(cmdrParseInt)
.default(5)
)
.option("--generate-silent-files", "generate silent files", false)
Expand Down
44 changes: 34 additions & 10 deletions test/command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@ jest.mock("audiomoth-utils", () => ({
describe("audiomoth-utils CLI", () => {
beforeEach(() => {
audiomothUtils.expand.mockClear();
// Mock console.log and console.error
console.log = jest.fn();
console.error = jest.fn();
// Mock process.exit
process.exit = jest.fn();
process.exitCode = jest.fn();
});

test("calling with no arguments uses default values", () => {
test("calling with no arguments is same as calling with explicit values (the defaults)", () => {
// This helps find bugs with CLI argument parsing, e.g. Github issue #1
audiomothUtils.expand.mockReturnValue({ success: true });

// No arguments - uses defaults
command.fab(["expand-duration", "input.wav"]);
// Hard-coding the defaults. This tests that are CLI args get parsed correctly.
command.fab(["expand-duration", "input.wav", "--max-file-duration=5"]);

expect(audiomothUtils.expand).toHaveBeenCalledTimes(2);
expect(audiomothUtils.expand.mock.calls[0]).toEqual(
audiomothUtils.expand.mock.calls[1]
);

expect(audiomothUtils.expand).toHaveBeenCalledWith(
"input.wav",
Expand All @@ -30,14 +37,31 @@ describe("audiomoth-utils CLI", () => {
false,
false
);
});

expect(console.log).toHaveBeenCalledWith(
"Expansion completed successfully."
test("setting boolean CLI arguments (flags)", () => {
audiomothUtils.expand.mockReturnValue({ success: true });
command.fab([
"expand-duration",
"input.wav",
"--generate-silent-files",
"--align-to-second-transitions",
]);

expect(audiomothUtils.expand).toHaveBeenCalledTimes(1);

expect(audiomothUtils.expand).toHaveBeenCalledWith(
"input.wav",
undefined,
undefined,
"DURATION",
5,
true,
true
);
expect(process.exitCode).toEqual(0);
});

test("audiomothUtils.expand returns success", () => {
test("when audiomothUtils.expand returns success", () => {
audiomothUtils.expand.mockReturnValue({ success: true });
command.fab(["expand-duration", "input.wav"]);

Expand All @@ -48,7 +72,7 @@ describe("audiomoth-utils CLI", () => {
expect(process.exitCode).toEqual(0);
});

test("audiomothUtils.expand returns failure", () => {
test("when audiomothUtils.expand returns failure", () => {
audiomothUtils.expand.mockReturnValue({
success: false,
error: "Invalid file or some such",
Expand All @@ -63,7 +87,7 @@ describe("audiomoth-utils CLI", () => {
expect(process.exitCode).toEqual(1);
});

test("audiomothUtils.expand throws an exception", () => {
test("when audiomothUtils.expand throws an exception", () => {
audiomothUtils.expand.mockImplementation(() => {
throw new Error("Unexpected error");
});
Expand Down

0 comments on commit af78b75

Please sign in to comment.