-
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.
- Loading branch information
Showing
6 changed files
with
86 additions
and
20 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { test } from "@cross/test"; | ||
import { find } from "./find.ts"; | ||
|
||
test("find locates find.test.ts file", async () => { | ||
const results = await find(".", (path) => path.endsWith("find.test.ts")); | ||
assertEquals(results.length > 0, true); | ||
assertEquals( | ||
results.find((path) => path.includes("/src/stat/find.test.ts"))?.includes( | ||
"/src/stat/find.test.ts", | ||
), | ||
true, | ||
); | ||
}); | ||
|
||
test("find doest not locate findnonexistant.test.ts file", async () => { | ||
const results = await find( | ||
".", | ||
(path) => path.endsWith("findnonexistant.test.ts"), | ||
); | ||
assertEquals(results.length === 0, true); | ||
assertEquals( | ||
results.find((path) => path.includes("/src/stat/findnonexistant.test.ts")), | ||
undefined, | ||
); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { test } from "@cross/test"; | ||
import { hash } from "./hash.ts"; | ||
|
||
test("hash is calculated correctly", async () => { | ||
const sha = await hash("./LICENSE"); | ||
assertEquals( | ||
sha, | ||
"f8c9819eb0c322eef28a0d0948877df068276f487b81326af842d3a20e7c9bbc", | ||
); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import crypto from "node:crypto"; | ||
import { readFile } from "../io/mod.ts"; | ||
|
||
/** | ||
* Calculates the MD5 hash of a file. | ||
* - uses node:crypto for widest compatibility | ||
* | ||
* @param filePath - The path to the file. | ||
* @param algorithm - The algorithm to use | ||
* @returns The hash as a hexadecimal string. | ||
*/ | ||
export async function hash( | ||
filePath: string, | ||
algorithm: string = "sha256", | ||
): Promise<string> { | ||
const hash = crypto.createHash(algorithm); | ||
const fileData = await readFile(filePath); | ||
hash.update(fileData); | ||
return hash.digest("hex"); | ||
} |
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,26 +1,29 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { test } from "@cross/test"; | ||
import { find } from "./find.ts"; | ||
|
||
test("find locates find.test.ts file", async () => { | ||
const results = await find(".", (path) => path.endsWith("find.test.ts")); | ||
assertEquals(results.length > 0, true); | ||
assertEquals( | ||
results.find((path) => path.includes("/src/stat/find.test.ts"))?.includes( | ||
"/src/stat/find.test.ts", | ||
), | ||
true, | ||
); | ||
import { isDir, isFile } from "./is.ts"; | ||
|
||
test("isFile returns true on existing file", async () => { | ||
const result = await isFile("./mod.ts"); | ||
assertEquals(result, true); | ||
}); | ||
|
||
test("isFile returns false on non-existiantfile", async () => { | ||
const result = await isFile("./mod-nonexistant.ts"); | ||
assertEquals(result, false); | ||
}); | ||
|
||
test("isDir returns false on existing file", async () => { | ||
const result = await isDir("./mod.ts"); | ||
assertEquals(result, false); | ||
}); | ||
|
||
test("isFile returns false on existing dir", async () => { | ||
const result = await isFile("./src"); | ||
assertEquals(result, false); | ||
}); | ||
|
||
test("find doest not locate findnonexistant.test.ts file", async () => { | ||
const results = await find( | ||
".", | ||
(path) => path.endsWith("findnonexistant.test.ts"), | ||
); | ||
assertEquals(results.length === 0, true); | ||
assertEquals( | ||
results.find((path) => path.includes("/src/stat/findnonexistant.test.ts")), | ||
undefined, | ||
); | ||
test("isDir returns true on existing dir", async () => { | ||
const result = await isDir("./src"); | ||
assertEquals(result, true); | ||
}); |
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