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

test coverage > 80% 🤞 #482

Merged
merged 2 commits into from
Mar 28, 2023
Merged
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
30 changes: 0 additions & 30 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ Array.prototype.compact_push = function<T>(item: T | null | undefined) {
if (item) this.push(item)
}

Array.prototype.compact_unshift = function<T>(item: T | null | undefined) {
if (item) this.unshift(item)
}

export function flatmap<S, T>(t: T | undefined | null, body: (t: T) => S | undefined, opts?: {rescue?: boolean}): NonNullable<S> | undefined {
try {
if (t) return body(t) ?? undefined
Expand All @@ -139,19 +135,6 @@ export async function async_flatmap<S, T>(t: Promise<T | undefined | null>, body
}
}

export function pivot<E, L, R>(input: E[], body: (e: E) => ['L', L] | ['R', R]): [L[], R[]] {
const rv = {
'L': [] as L[],
'R': [] as R[]
}
for (const e of input) {
const [side, value] = body(e)
// deno-lint-ignore no-explicit-any
rv[side].push(value as any)
}
return [rv['L'], rv['R']]
}

declare global {
interface Promise<T> {
swallow(err?: unknown): Promise<T | undefined>
Expand Down Expand Up @@ -179,24 +162,11 @@ Promise.prototype.swallow = function(gristle?: unknown) {
})
}

export async function attempt<T>(body: () => Promise<T>, opts: {swallow: unknown}): Promise<T | undefined> {
try {
return await body()
} catch (err) {
if (err !== opts.swallow) throw err
}
}

///////////////////////////////////////////////////////////////////////// misc
import TeaError, { UsageError, panic } from "./error.ts"
export { TeaError, UsageError, panic }
export * as error from "./error.ts"

// deno-lint-ignore no-explicit-any
export function tuplize<T extends any[]>(...elements: T) {
return elements
}

///////////////////////////////////////////////////////////////////////// pkgs
export * as pkg from "./pkg.ts"

Expand Down
10 changes: 8 additions & 2 deletions src/vendor/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,15 @@ export default class Path {
return Deno.readTextFile(this.string)
}

readLines(): AsyncIterableIterator<string> {
async *readLines(): AsyncIterableIterator<string> {
const fd = Deno.openSync(this.string)
return readLines(fd)
try {
for await (const line of readLines(fd))
yield line
}
finally {
fd.close()
}
}

//FIXME like, we don’t want a hard dependency in the published library
Expand Down
84 changes: 84 additions & 0 deletions tests/unit/path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import Path from "path";
import { assert, assertEquals, assertFalse, assertThrows } from "deno/testing/asserts.ts"

Deno.test("test Path", async test => {
await test.step("creating files", () => {
assertEquals(new Path("/a/b/c").components(), ["", "a", "b", "c"])
assertEquals(new Path("/a/b/c").split(), [new Path("/a/b"), "c"])

const tmp = Path.mktmp({prefix: "tea-"})
assert(tmp.isEmpty())

const child = tmp.join("a/b/c")
assertFalse(child.parent().isDirectory())
child.mkparent()
assert(child.parent().isDirectory())

assertThrows(() => child.readlink()) // not found
assertFalse(child.isReadableFile())
child.touch()
assert(child.isReadableFile())

assert(child.in(tmp))
assertFalse(tmp.isEmpty())
assertEquals(child.readlink(), child) // not a link
})

await test.step("write and read", async () => {
const tmp = Path.mktmp({prefix: "tea-"})

const data = tmp.join("test.dat")
data.write({text: "hello\nworld"})

const lines = await asyncIterToArray(data.readLines())
assertEquals(lines, ["hello", "world"])

// will throw with no force flag
assertThrows(() => data.write({ json: { hello: "world" } }))

data.write({ json: { hello: "world" }, force: true })
assertEquals(await data.readJSON(), { hello: "world" })
})

await test.step("test walk", async () => {
const tmp = Path.mktmp({prefix: "tea-"})

const a = tmp.join("a").mkdir()
a.join("a1").touch()
a.join("a2").touch()

const b = tmp.join("b").mkdir()
b.join("b1").touch()
b.join("b2").touch()

const c = tmp.join("c").mkdir()
c.join("c1").touch()
c.join("c2").touch()

const walked = (await asyncIterToArray(tmp.walk()))
.map(([path, entry]) => {
return {name: path.basename(), isDir: entry.isDirectory}
})
.sort((a, b) => a.name.localeCompare(b.name))

assertEquals(walked, [
{ name: "a", isDir: true},
{ name: "a1", isDir: false},
{ name: "a2", isDir: false},
{ name: "b", isDir: true},
{ name: "b1", isDir: false},
{ name: "b2", isDir: false},
{ name: "c", isDir: true},
{ name: "c1", isDir: false},
{ name: "c2", isDir: false},
])
})
})

async function asyncIterToArray<T> (iter: AsyncIterable<T>){
const result = [];
for await(const i of iter) {
result.push(i);
}
return result;
}