Skip to content
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

Switch from Deno.run to Deno.Command #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions src/local-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@ const findRelativeFile = (
*/
export const getCommandline = function (
mainModule: string,
denoExecutablePath: string,
// deno-lint-ignore no-explicit-any
manifest: any,
devDomain: string,
hookCLI: Protocol,
): string[] {
const command = [
denoExecutablePath,
"run",
"-q",
"--config=deno.jsonc",
Expand Down Expand Up @@ -121,15 +119,18 @@ export const runWithOutgoingDomains = async function (

const command = getCommandline(
Deno.mainModule,
denoExecutablePath,
manifest,
devDomain,
hookCLI,
);

const p = Deno.run({ cmd: command });
const commander = new Deno.Command(denoExecutablePath, {
args: command,
});

const subprocess = commander.spawn();
const status = await subprocess.status;
Comment on lines +131 to +132
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

One thing we should test around this is whether or not this leaves the process open or if we should close via something like subprocess.kill()

Copy link
Contributor

Choose a reason for hiding this comment

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

Generally, once the status code from a process is able to be retrieved, this implies that the process completed, so my expectation is that we don't need to do that - but doesn't hurt!


const status = await p.status();
if (!status.success) {
Deno.exit(status.code);
}
Expand Down
50 changes: 18 additions & 32 deletions src/tests/local-run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const fakeManifest = (...domains: string[]) => {
};
};

const FAKE_DENO_PATH = "/path/to/deno";

const FAKE_DENO_LAND_MODULE =
"https://deno.land/x/[email protected]/local-run.ts";
const FAKE_DENO_LAND_EXPECTED_MODULE =
Expand All @@ -31,13 +29,11 @@ Deno.test("getCommandline function", async (t) => {
await t.step("issues right command no dev domain", () => {
const command = getCommandline(
FAKE_DENO_LAND_MODULE,
FAKE_DENO_PATH,
fakeManifest("example.com"),
"",
MockProtocol(),
);
assertEquals(command, [
FAKE_DENO_PATH,
"run",
"-q",
"--config=deno.jsonc",
Expand All @@ -51,13 +47,11 @@ Deno.test("getCommandline function", async (t) => {
await t.step("issues right command with dev domain", () => {
const command = getCommandline(
FAKE_DENO_LAND_MODULE,
FAKE_DENO_PATH,
fakeManifest("example.com"),
"dev1234.slack.com",
MockProtocol(),
);
assertEquals(command, [
FAKE_DENO_PATH,
"run",
"-q",
"--config=deno.jsonc",
Expand All @@ -72,13 +66,11 @@ Deno.test("getCommandline function", async (t) => {
await t.step("issues right command with no outgoing domains", () => {
const command = getCommandline(
FAKE_DENO_LAND_MODULE,
FAKE_DENO_PATH,
fakeManifest(),
"",
MockProtocol(),
);
assertEquals(command, [
FAKE_DENO_PATH,
"run",
"-q",
"--config=deno.jsonc",
Expand All @@ -92,13 +84,11 @@ Deno.test("getCommandline function", async (t) => {
await t.step("issues right command with a local file module", () => {
const command = getCommandline(
FAKE_FILE_MODULE,
FAKE_DENO_PATH,
fakeManifest(),
"",
MockProtocol(),
);
assertEquals(command, [
FAKE_DENO_PATH,
"run",
"-q",
"--config=deno.jsonc",
Expand All @@ -112,13 +102,11 @@ Deno.test("getCommandline function", async (t) => {
await t.step("handles root paths", () => {
const command = getCommandline(
"file:///local-run.ts",
FAKE_DENO_PATH,
fakeManifest("example.com"),
"",
MockProtocol(),
);
assertEquals(command, [
FAKE_DENO_PATH,
"run",
"-q",
"--config=deno.jsonc",
Expand All @@ -134,13 +122,11 @@ Deno.test("getCommandline function", async (t) => {
protocol.getCLIFlags = () => ["--mycustomflag"];
const command = getCommandline(
"file:///local-run.ts",
FAKE_DENO_PATH,
fakeManifest("example.com"),
"",
protocol,
);
assertEquals(command, [
FAKE_DENO_PATH,
"run",
"-q",
"--config=deno.jsonc",
Expand Down Expand Up @@ -188,22 +174,22 @@ Deno.test("runWithOutgoingDomains function", async (t) => {
const execPathSpy = mock.stub(Deno, "execPath", () => {
throw new Error("no idea where that is");
});
const runStub = mock.stub(
const commandStub = mock.stub(
Deno,
"run",
"Command",
() => ({
status: () =>
Promise.resolve({
success: true,
code: 0,
}),
} as unknown as Deno.Process<Deno.RunOptions>),
spawn: () => {
return {
status: Promise.resolve({ success: true, code: 0 }),
} as unknown as Deno.ChildProcess;
},
} as Deno.Command),
);
try {
await runWithOutgoingDomains(createEmptyManifest, "", protocol);
} finally {
execPathSpy.restore();
runStub.restore();
commandStub.restore();
}
mock.assertSpyCallArg(
errorSpy,
Expand All @@ -230,21 +216,21 @@ Deno.test("runWithOutgoingDomains function", async (t) => {
exitSpy as unknown as () => never,
);
const exitCode = 1337;
const runStub = mock.stub(
const commandStub = mock.stub(
Deno,
"run",
"Command",
() => ({
status: () =>
Promise.resolve({
success: false,
code: exitCode,
}),
} as unknown as Deno.Process<Deno.RunOptions>),
spawn: () => {
return {
status: Promise.resolve({ success: false, code: exitCode }),
} as unknown as Deno.ChildProcess;
},
} as Deno.Command),
);
try {
await runWithOutgoingDomains(createEmptyManifest, "", protocol);
} finally {
runStub.restore();
commandStub.restore();
exitStub.restore();
}
mock.assertSpyCallArg(
Expand Down