-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: Use Deno.Command instead of Deno.run - bump minimum Deno versio…
…n to v1.31.1 (#16) * use Deno Command * revert checking code * Add test for no process found * Add comment
- Loading branch information
Showing
6 changed files
with
83 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,106 @@ | ||
import { assertEquals, assertNotEquals, delay } from "./deps_test.ts"; | ||
import { | ||
assertEquals, | ||
assertNotEquals, | ||
assertStringIncludes, | ||
delay, | ||
} from "./deps_test.ts"; | ||
|
||
Deno.test("killing by pid", async () => { | ||
// create pid | ||
const pTest = Deno.run({ | ||
cmd: ["sleep", "5000"], | ||
// create test process | ||
const command = new Deno.Command("sleep", { | ||
args: [ | ||
"5000", | ||
], | ||
}); | ||
const pTest = command.spawn(); | ||
await delay(100); | ||
|
||
// call dkill | ||
const pDkill = Deno.run({ | ||
cmd: ["deno", "run", "-A", "./cli.ts", `${pTest.pid}`], | ||
const pKill = new Deno.Command(Deno.execPath(), { | ||
args: [ | ||
"run", | ||
"-A", | ||
"./cli.ts", | ||
`${pTest.pid}`, | ||
], | ||
}); | ||
// wait dkill finishes | ||
const cliStatus = await pDkill.status(); | ||
const { code } = await pKill.output(); | ||
|
||
// retreive status from test pid | ||
const status = await pTest.status(); | ||
|
||
// close resources | ||
pTest.close(); | ||
pDkill.close(); | ||
const status = await pTest.status; | ||
|
||
// ensure dkill existed cleanly | ||
assertEquals(cliStatus.code, 0); | ||
assertEquals(code, 0); | ||
assertNotEquals(status.code, 0); | ||
}); | ||
|
||
Deno.test({ | ||
name: "killing by ports", | ||
fn: async () => { | ||
// create a webserver | ||
const pTest1 = Deno.run({ | ||
cmd: ["deno", "run", "-A", "./src/tests/utils.ts"], | ||
// create 2 webservers | ||
const pTest1Cmd = new Deno.Command(Deno.execPath(), { | ||
args: [ | ||
"run", | ||
"-A", | ||
"./src/tests/utils.ts", | ||
], | ||
}); | ||
const pTest2 = Deno.run({ | ||
cmd: ["deno", "run", "-A", "./src/tests/utils.ts", "8081"], | ||
|
||
const pTest2Cmd = new Deno.Command(Deno.execPath(), { | ||
args: [ | ||
"run", | ||
"-A", | ||
"./src/tests/utils.ts", | ||
"8081", | ||
], | ||
}); | ||
const pTest1 = pTest1Cmd.spawn(); | ||
const pTest2 = pTest2Cmd.spawn(); | ||
|
||
// give time fo the webserver to start and the port be discoverable | ||
await delay(5000); | ||
|
||
// call dkill | ||
const pDkill = Deno.run({ | ||
cmd: [ | ||
"deno", | ||
const pDkillCmd = new Deno.Command(Deno.execPath(), { | ||
args: [ | ||
"run", | ||
"-A", | ||
"--unstable", | ||
"./cli.ts", | ||
"--verbose", | ||
":8080", | ||
":8081", | ||
], | ||
}); | ||
|
||
// wait dkill finishes | ||
const cliStatus = await pDkill.status(); | ||
pDkill.close(); | ||
const { code } = await pDkillCmd.output(); | ||
// ensure dkill exited cleanly | ||
assertEquals(cliStatus.code, 0); | ||
|
||
// throw Error('xxx') | ||
assertEquals(code, 0); | ||
|
||
// retrieve status from test pid | ||
const status1 = await pTest1.status(); | ||
const status2 = await pTest2.status(); | ||
// close resources | ||
pTest1.close(); | ||
pTest2.close(); | ||
const status1 = await pTest1.status; | ||
const status2 = await pTest2.status; | ||
|
||
assertNotEquals(status1.code, 0); | ||
assertNotEquals(status1.code, 5); // check it wasn't a timeout | ||
assertNotEquals(status2.code, 0); | ||
assertNotEquals(status2.code, 5); | ||
}, | ||
}); | ||
|
||
Deno.test("Invalid ports", async () => { | ||
// call dkill | ||
const pDkillCmd = new Deno.Command(Deno.execPath(), { | ||
args: [ | ||
"run", | ||
"-A", | ||
"./cli.ts", | ||
":8080", | ||
], | ||
}); | ||
|
||
// wait dkill finishes | ||
const { code, stdout } = await pDkillCmd.output(); | ||
// ensure dkill exited cleanly | ||
assertEquals(code, 0); | ||
assertStringIncludes(new TextDecoder().decode(stdout), "No process found"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
/** | ||
* Return the stdout of the command. | ||
* This does not throw if the code return is non zero. | ||
*/ | ||
export async function runCmd(cmd: string[], verbose?: boolean) { | ||
verbose && console.log(cmd.join(" ")); | ||
const exec = Deno.run({ | ||
cmd, | ||
stdout: "piped", | ||
stderr: "piped", | ||
const [cmd1, ...cmd2] = cmd; | ||
|
||
const exec = new Deno.Command(cmd1, { | ||
args: cmd2, | ||
stderr: "inherit", | ||
}); | ||
|
||
const out = await exec.output(); | ||
exec.close(); | ||
return new TextDecoder().decode(out); | ||
const { stdout } = await exec.output(); | ||
return new TextDecoder().decode(stdout); | ||
} |