|
1 | 1 | // deno test --allow-read --allow-write
|
2 | 2 | import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
|
3 |
| -import { parseFileContent } from "../lib/file.ts"; |
4 |
| - |
5 |
| -Deno.test("Read file contents test", async () => { |
6 |
| - const filePath = "./file1.txt"; |
7 |
| - const expectedContent = "This is the content of file1."; |
8 |
| - |
9 |
| - try { |
10 |
| - await Deno.writeTextFile(filePath, expectedContent); |
11 |
| - const fileContent = await parseFileContent(filePath); |
12 |
| - |
13 |
| - assertEquals( |
14 |
| - fileContent.toString(), |
15 |
| - "```" + filePath + "\n" + expectedContent + "\n```", |
16 |
| - ); |
17 |
| - } catch (error) { |
18 |
| - console.error("Error during test:", error); |
19 |
| - throw error; |
20 |
| - } finally { // Clean up test files |
| 3 | +import { exists } from "jsr:@std/fs/exists"; |
| 4 | +import { expect } from "jsr:@std/expect"; |
| 5 | +import { |
| 6 | + afterAll, |
| 7 | + beforeAll, |
| 8 | + describe, |
| 9 | + it, |
| 10 | +} from "https://deno.land/std/testing/bdd.ts"; |
| 11 | + |
| 12 | +import { filesGenerator, parseFileContent } from "../lib/file.ts"; |
| 13 | + |
| 14 | +describe("create test directory", async () => { |
| 15 | + const tempDir = new URL("./temp/", import.meta.url); |
| 16 | + beforeAll(async () => { |
| 17 | + // 絶対パスで一時ディレクトリを作成 |
| 18 | + if (!await exists(tempDir)) { |
| 19 | + await Deno.mkdir(tempDir, { recursive: true }); |
| 20 | + } |
| 21 | + const testFiles = ["file.txt", "dir", "test.js", "test.ts"]; |
| 22 | + |
| 23 | + // 一時ファイルを作成 |
| 24 | + for (const file of testFiles) { |
| 25 | + const filePath = new URL(file, tempDir); |
| 26 | + await Deno.writeTextFile(filePath, ""); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + afterAll(() => { |
| 31 | + Deno.removeSync(tempDir, { recursive: true }); |
| 32 | + }); |
| 33 | + |
| 34 | + it("Read file contents test", async () => { |
| 35 | + const filePath = "./file1.txt"; |
| 36 | + const expectedContent = "This is the content of file1."; |
| 37 | + |
21 | 38 | try {
|
22 |
| - await Deno.remove(filePath); |
23 |
| - } catch (cleanupError) { |
24 |
| - console.error("Error cleaning up test files:", cleanupError); |
| 39 | + await Deno.writeTextFile(filePath, expectedContent); |
| 40 | + const fileContent = await parseFileContent(filePath); |
| 41 | + |
| 42 | + assertEquals( |
| 43 | + fileContent.toString(), |
| 44 | + "```" + filePath + "\n" + expectedContent + "\n```", |
| 45 | + ); |
| 46 | + } catch (error) { |
| 47 | + console.error("Error during test:", error); |
| 48 | + throw error; |
| 49 | + } finally { // Clean up test files |
| 50 | + try { |
| 51 | + await Deno.remove(filePath); |
| 52 | + } catch (cleanupError) { |
| 53 | + console.error("Error cleaning up test files:", cleanupError); |
| 54 | + } |
25 | 55 | }
|
26 |
| - } |
| 56 | + }); |
| 57 | + |
| 58 | + it("非Globパターンはそのまま生成される", async () => { |
| 59 | + const patterns = ["test.js", "dir"]; |
| 60 | + const generator = filesGenerator(patterns); |
| 61 | + const result: string[] = []; |
| 62 | + |
| 63 | + for await (const file of generator) { |
| 64 | + result.push(file); |
| 65 | + } |
| 66 | + |
| 67 | + assertEquals(result, ["test.js", "dir"]); |
| 68 | + }); |
| 69 | + |
| 70 | + it("Globパターンが正しく展開される", async () => { |
| 71 | + const patterns = ["./test/**/*.txt", "./test/**/*.js"]; |
| 72 | + const generator = filesGenerator(patterns); |
| 73 | + const result: string[] = []; |
| 74 | + |
| 75 | + for await (const file of generator) { |
| 76 | + result.push(file); |
| 77 | + } |
| 78 | + |
| 79 | + expect(result).toContain(Deno.cwd() + "/test/temp/file.txt"); |
| 80 | + expect(result).toContain(Deno.cwd() + "/test/temp/test.js"); |
| 81 | + }); |
27 | 82 | });
|
0 commit comments