Skip to content

Commit

Permalink
++coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Oct 20, 2023
1 parent 5d843d8 commit 897a3e3
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 18 deletions.
22 changes: 9 additions & 13 deletions src/hooks/useConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,22 @@ export default function useConfig(input?: Config): Config {

function boolize(input: string | undefined): boolean | undefined {
switch (input?.trim()?.toLowerCase()) {
case '0':
case 'false':
case 'no':
return false
case '1':
case 'true':
case 'yes':
return true
case '0':
case 'false':
case 'no':
return false
case '1':
case 'true':
case 'yes':
return true
}
}

function reset() {
return delete gt.sh_pkgx_config
}

function initialized() {
return gt.sh_pkgx_config !== undefined
}

export const _internals = { reset, initialized, boolize }
export const _internals = { initialized, boolize }


/// we support a pkgx installed or system installed git, nothing else
Expand Down
84 changes: 84 additions & 0 deletions src/hooks/useInventory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// deno-lint-ignore-file require-await no-explicit-any
import { assertEquals } from "deno/assert/assert_equals.ts"
import SemVer, * as semver from "../utils/semver.ts"
import { assertRejects } from "deno/assert/mod.ts"
import * as mock from "deno/testing/mock.ts"
import { _internals } from "./useFetch.ts"
import specimen from "./useInventory.ts"

Deno.test("useInventory", async runner => {
await runner.step("select()", async () => {
const stub = mock.stub(_internals, "fetch", async () => {
return {
ok: true,
status: 200,
async text() {
return "1.2.3\n1.2.4"
}
} as any
})

try {
assertEquals(
(await specimen().select({project: "foo", version: new SemVer("1.2.3")}))?.toString(),
"1.2.3"
)

assertEquals(
(await specimen().select({project: "foo", constraint: new semver.Range("=1.2.3")}))?.toString(),
"1.2.3"
)
} finally {
stub.restore()
}
})

await runner.step("fail HTTP", async () => {
const stub = mock.stub(_internals, "fetch", async () => {
return {
ok: false,
status: 404
} as any
})

try {
assertRejects(() => specimen().select({project: "foo", version: new SemVer("1.2.3")}))
} finally {
stub.restore()
}
})

await runner.step("fail no versions", async () => {
const stub = mock.stub(_internals, "fetch", async () => {
return {
ok: true,
status: 200,
async text() { return "" }
} as any
})

try {
assertRejects(() => specimen().select({project: "foo", version: new SemVer("1.2.3")}))
} finally {
stub.restore()
}
})

await runner.step("openssl hack", async () => {
const stub = mock.stub(_internals, "fetch", async () => {
return {
ok: true,
status: 200,
async text() { return "1.1.118\n1.1.117" }
} as any
})

try {
assertEquals(
(await specimen().select({project: "openssl.org", constraint: new semver.Range("^1")}))?.toString(),
"1.1.117")
} finally {
stub.restore()
}
})
})
4 changes: 2 additions & 2 deletions src/hooks/useInventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ const get = async (rq: PackageRequirement | Package) => {
}

const releases = await rsp.text()
let versions = releases.split("\n").compact(x => new SemVer(x))
let versions = releases ? releases.split("\n").compact(x => new SemVer(x)) : []

if (versions.length < 1) throw new Error()
if (versions.length < 1) throw new Error(`No versions for ${rq.project}`)

if (rq.project == 'openssl.org') {
// workaround our previous sins
Expand Down
24 changes: 21 additions & 3 deletions src/porcelain/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useTestConfig } from "../hooks/useTestConfig.ts"
import { assertEquals } from "deno/assert/mod.ts"
import { assertEquals, assertRejects } from "deno/assert/mod.ts"
import undent from "outdent"
import run from "./run.ts"

Expand All @@ -12,21 +12,39 @@ Deno.test("porcelain.run", async runner => {
if (Deno.build.os != 'windows') {
foo.join("foo").write({ text: undent`
#!/bin/sh
if [ "$1" = "--fail" ]; then exit 1; fi
echo "abcdef--"
echo "ghijkl--" 1>&2
`}).chmod(0o755)
} else {
foo.join("foo.bat").write({text: '@echo abcdef--'})
foo.join("foo.bat").write({text: undent`
@echo off
IF "%~1"=="--fail" ( exit /b 1 )
echo abcdef--
echo ghijkl-- 1>&2
`})
}

prefix.join("bar.com/v1.2.3").mkdir('p').join("not-empty").touch()

await runner.step("std", async () => {
await run("foo --args")
await run("foo") // tests no spaces branch

await assertRejects(() => run([]))
})

await runner.step("stdout", async () => {
await runner.step("std(out|err)", async () => {
const { stdout } = await run(["foo", "--args"], {stdout: true})
const nl = Deno.build.os === "windows" ? "\r\n" : "\n";
assertEquals(stdout, `abcdef--${nl}`)

const { stderr } = await run(["foo", "--args"], {stderr: true})
const expected = Deno.build.os === "windows" ? 'ghijkl-- \r\n' : 'ghijkl--\n'
assertEquals(stderr, expected)
})

await runner.step("cmd fails", async () => {
await assertRejects(() => run(["foo", "--fail"]))
})
})

0 comments on commit 897a3e3

Please sign in to comment.