From 464280737e541c6ec1a21bf97e626dceb29fdaad Mon Sep 17 00:00:00 2001 From: vkumbhar94 Date: Wed, 5 Mar 2025 23:16:00 +0530 Subject: [PATCH] fix(path): add util function to read multi document yaml file --- fixtures/pathtests/readYAMLAll.yaml | 3 +++ src/deps.ts | 4 +-- src/utils/Path.test.ts | 38 ++++++++++++++++++++++++++++- src/utils/Path.ts | 14 ++++++++++- 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 fixtures/pathtests/readYAMLAll.yaml diff --git a/fixtures/pathtests/readYAMLAll.yaml b/fixtures/pathtests/readYAMLAll.yaml new file mode 100644 index 0000000..7634676 --- /dev/null +++ b/fixtures/pathtests/readYAMLAll.yaml @@ -0,0 +1,3 @@ +abc: xyz +--- +ijk: lmn \ No newline at end of file diff --git a/src/deps.ts b/src/deps.ts index adfe1d0..42d1d1a 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -11,11 +11,11 @@ export { outdent } import * as crypto from "jsr:@std/crypto@1" import { moveSync } from "jsr:@std/fs@1" import { writeAll } from "jsr:@std/io@^0.225.0" -import { parse as parseYaml } from "jsr:@std/yaml@1" +import { parse as parseYaml, parseAll as parseYamlALL } from "jsr:@std/yaml@1" import { SEPARATOR as SEP, fromFileUrl } from "jsr:@std/path@1" const streams = { writeAll } const fs = { moveSync } -const deno = { crypto, fs, streams, parseYaml, SEP, fromFileUrl } +const deno = { crypto, fs, streams, parseYaml, parseYamlALL, SEP, fromFileUrl } export { deno } diff --git a/src/utils/Path.test.ts b/src/utils/Path.test.ts index 11327bc..7c644d5 100644 --- a/src/utils/Path.test.ts +++ b/src/utils/Path.test.ts @@ -1,4 +1,4 @@ -import { assert, assertEquals, assertFalse, assertThrows } from "@std/assert" +import { assert, assertEquals, assertFalse, assertThrows, fail } from "@std/assert" import { SEPARATOR as SEP } from "jsr:@std/path@1" import Path from "./Path.ts" @@ -199,6 +199,42 @@ Deno.test("Path.prettyLocalString()", () => { assertEquals(new Path("/a/b").prettyLocalString(), `${root}a${SEP}b`) }) +Deno.test("Path.readYAMLAll()", async () => { + const path = Path.cwd().join("./fixtures/pathtests/readYAMLAll.yaml"); + + try { + const yamlData = await path.readYAMLAll(); // ✅ Use await + + assertEquals(Array.isArray(yamlData), true, "Expected yamlData to be an array"); + + if (!Array.isArray(yamlData)) { + fail("Expected an array"); + return; + } + + assertEquals(yamlData.length, 2, "Expected exactly 2 YAML documents"); + assertEquals(yamlData, [{ abc: "xyz" }, { ijk: "lmn" }], "YAML content mismatch"); + + } catch (err) { + console.error("Error reading YAML:", err); + fail("Error reading YAML"); + } +}); + +Deno.test("Path.readYAMLAllErr()", async () => { + const path = Path.cwd().join("./fixtures/pathtests/invalid.yaml"); + try { + await path.readYAMLAll(); + fail("invalid file should not reach here") + } catch (err) { + if (err instanceof Error) { + assertEquals(err.name, "NotFound") + } else{ + throw err; + } + } +}); + Deno.test("Path.chuzzle()", () => { const path = Path.mktemp().join("file.txt").touch() assertEquals(path.chuzzle(), path) diff --git a/src/utils/Path.ts b/src/utils/Path.ts index 8391afa..619411b 100644 --- a/src/utils/Path.ts +++ b/src/utils/Path.ts @@ -4,7 +4,7 @@ import { mkdtempSync } from "node:fs" import * as sys from "node:path" import * as os from "node:os" -const { fs, parseYaml, SEP } = deno +const { fs, parseYaml, parseYamlALL, SEP } = deno // modeled after https://github.com/mxcl/Path.swift @@ -411,6 +411,18 @@ export default class Path { } } + async readYAMLAll(): Promise { + try { + const txt = await this.read() + return parseYamlALL(txt) + } catch (err) { + if (err instanceof Error) { + err.cause = this.string + } + throw err + } + } + readJSON(): Promise { return this.read().then(x => JSON.parse(x)) }