Skip to content

Prioritizing --help arg handling #6667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

- Build with OCaml 5.1.1. https://github.com/rescript-lang/rescript-compiler/pull/6641

#### :nail_care: Polish

- Make the `--help` arg be prioritized in the CLI, so correctly prints help message and skip other commands. https://github.com/rescript-lang/rescript-compiler/pull/6667

# 11.1.0-rc.3

#### :nail_care: Polish
Expand Down
356 changes: 183 additions & 173 deletions jscomp/build_tests/cli_help/input.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check

const assert = require("assert");
const child_process = require("child_process");
const { exec } = require("../utils.js");

const cliHelp =
"Usage: rescript <options> <subcommand>\n" +
Expand Down Expand Up @@ -72,194 +72,204 @@ const dumpHelp =
"Usage: rescript dump <options> [target]\n" +
"`rescript dump` dumps the information for the target\n";

// Shows build help with --help arg
let out = child_process.spawnSync(`../../../rescript`, ["build", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, buildHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
async function test() {
{
// Shows build help with --help arg
const out = await exec(`../../../rescript`, ["build", "--help"], {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are already refactoring this, we could add a helper function in this file so that we can just write

const out = await execReScript(["build", "--help"]);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I separate it into a separate pure refactor PR?

Aside from the cli_help test, there are many other places to change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, of course!

cwd: __dirname,
});
assert.equal(out.stdout, buildHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// FIXME: Help works incorrectly in watch mode
out = child_process.spawnSync(`../../../rescript`, ["build", "-w", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
// FIXME: Shouldn't have "Start compiling" for help
assert.equal(out.stdout, ">>>> Start compiling\n" + buildHelp);
// FIXME: Don't run the watcher when showing help
assert.match(
out.stderr,
new RegExp(
"Uncaught Exception Error: ENOENT: no such file or directory, watch 'bsconfig.json'\n"
)
);
// FIXME: Should be 0
assert.equal(out.status, 1);
{
const out = await exec(`../../../rescript`, ["build", "-w", "--help"], {
cwd: __dirname,
});
assert.equal(out.stdout, buildHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// FIXME: Has the same problem with `rescript -w`
out = child_process.spawnSync(`../../../rescript`, ["-w", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, ">>>> Start compiling\n" + buildHelp);
assert.match(
out.stderr,
new RegExp(
"Uncaught Exception Error: ENOENT: no such file or directory, watch 'bsconfig.json'\n"
)
);
{
const out = await exec(`../../../rescript`, ["-w", "--help"], {
cwd: __dirname,
});
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// Shows cli help with --help arg even if there are invalid arguments after it
out = child_process.spawnSync(`../../../rescript`, ["--help", "-w"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
{
// Shows cli help with --help arg even if there are invalid arguments after it
const out = await exec(`../../../rescript`, ["--help", "-w"], {
cwd: __dirname,
});
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// Shows build help with -h arg
out = child_process.spawnSync(`../../../rescript`, ["build", "-h"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, buildHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
{
// Shows build help with -h arg
const out = await exec(`../../../rescript`, ["build", "-h"], {
cwd: __dirname,
});
assert.equal(out.stdout, buildHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// Exits with build help with unknown arg
out = child_process.spawnSync(`../../../rescript`, ["build", "-foo"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, "");
assert.equal(out.stderr, 'Error: Unknown option "-foo".\n' + buildHelp);
assert.equal(out.status, 2);
{
// Exits with build help with unknown arg
const out = await exec(`../../../rescript`, ["build", "-foo"], {
cwd: __dirname,
});
assert.equal(out.stdout, "");
assert.equal(out.stderr, 'Error: Unknown option "-foo".\n' + buildHelp);
assert.equal(out.status, 2);
}

// Shows cli help with --help arg
out = child_process.spawnSync(`../../../rescript`, ["--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
{
// Shows cli help with --help arg
const out = await exec(`../../../rescript`, ["--help"], {
cwd: __dirname,
});
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// Shows cli help with -h arg
out = child_process.spawnSync(`../../../rescript`, ["-h"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
{
// Shows cli help with -h arg
const out = await exec(`../../../rescript`, ["-h"], {
cwd: __dirname,
});
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// Shows cli help with help command
out = child_process.spawnSync(`../../../rescript`, ["help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
{
// Shows cli help with -h arg
const out = await exec(`../../../rescript`, ["help"], {
cwd: __dirname,
});
assert.equal(out.stdout, cliHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// Exits with cli help with unknown command
out = child_process.spawnSync(`../../../rescript`, ["built"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, "");
assert.equal(out.stderr, `Error: Unknown command "built".\n` + cliHelp);
assert.equal(out.status, 2);
{
// Exits with cli help with unknown command
const out = await exec(`../../../rescript`, ["built"], {
cwd: __dirname,
});
assert.equal(out.stdout, "");
assert.equal(out.stderr, `Error: Unknown command "built".\n` + cliHelp);
assert.equal(out.status, 2);
}

// Exits with build help with unknown args
out = child_process.spawnSync(`../../../rescript`, ["-foo"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, "");
assert.equal(out.stderr, 'Error: Unknown option "-foo".\n' + buildHelp);
assert.equal(out.status, 2);
{
// Exits with build help with unknown args
const out = await exec(`../../../rescript`, ["-foo"], {
cwd: __dirname,
});
assert.equal(out.stdout, "");
assert.equal(out.stderr, 'Error: Unknown option "-foo".\n' + buildHelp);
assert.equal(out.status, 2);
}

// Shows clean help with --help arg
out = child_process.spawnSync(`../../../rescript`, ["clean", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, cleanHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
{
// Shows clean help with --help arg
const out = await exec(`../../../rescript`, ["clean", "--help"], {
cwd: __dirname,
});
assert.equal(out.stdout, cleanHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// Shows clean help with -h arg
out = child_process.spawnSync(`../../../rescript`, ["clean", "-h"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, cleanHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
{
// Shows clean help with -h arg
const out = await exec(`../../../rescript`, ["clean", "-h"], {
cwd: __dirname,
});
assert.equal(out.stdout, cleanHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// Exits with clean help with unknown arg
out = child_process.spawnSync(`../../../rescript`, ["clean", "-foo"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, "");
assert.equal(out.stderr, 'Error: Unknown option "-foo".\n' + cleanHelp);
assert.equal(out.status, 2);
{
// Exits with clean help with unknown arg
const out = await exec(`../../../rescript`, ["clean", "-foo"], {
cwd: __dirname,
});
assert.equal(out.stdout, "");
assert.equal(out.stderr, 'Error: Unknown option "-foo".\n' + cleanHelp);
assert.equal(out.status, 2);
}

// Shows format help with --help arg
out = child_process.spawnSync(`../../../rescript`, ["format", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, formatHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
{
// Shows format help with --help arg
const out = await exec(`../../../rescript format`, ["--help"], {
cwd: __dirname,
});
assert.equal(out.stdout, formatHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// Shows format help with -h arg
out = child_process.spawnSync(`../../../rescript`, ["format", "-h"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, formatHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
{
// Shows format help with -h arg
const out = await exec(`../../../rescript format`, ["-h"], {
cwd: __dirname,
});
assert.equal(out.stdout, formatHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// Shows convert help with --help arg
out = child_process.spawnSync(`../../../rescript`, ["convert", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, convertHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
{
// Shows convert help with --help arg
const out = await exec(`../../../rescript convert`, ["--help"], {
cwd: __dirname,
});
assert.equal(out.stdout, convertHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// Shows convert help with -h arg
out = child_process.spawnSync(`../../../rescript`, ["convert", "-h"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, convertHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
{
// Shows convert help with -h arg
const out = await exec(`../../../rescript convert`, ["-h"], {
cwd: __dirname,
});
assert.equal(out.stdout, convertHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// Shows dump help with --help arg
out = child_process.spawnSync(`../../../rescript`, ["dump", "--help"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, dumpHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
{
// Shows dump help with --help arg
const out = await exec(`../../../rescript dump`, ["--help"], {
cwd: __dirname,
});
assert.equal(out.stdout, dumpHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}

// Shows dump help with -h arg
out = child_process.spawnSync(`../../../rescript`, ["dump", "-h"], {
encoding: "utf8",
cwd: __dirname,
});
assert.equal(out.stdout, dumpHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
{
// Shows dump help with -h arg
const out = await exec(`../../../rescript dump`, ["-h"], {
cwd: __dirname,
});
assert.equal(out.stdout, dumpHelp);
assert.equal(out.stderr, "");
assert.equal(out.status, 0);
}
}

void test();
Loading