Path: blob/main/tests/unit/paths/qualified-path.test.ts
6451 views
/*1* qualified-path.test.ts2*3* Copyright (C) 2022 Posit Software, PBC4*5*/67import { unitTest } from "../../test.ts";89import {10makePath,11PathInfo,12QualifiedPath,13} from "../../../src/core/qualified-path.ts";14import { assertEquals, assertRejects } from "testing/asserts";15import { isWindows } from "../../../src/deno_ral/platform.ts";1617//deno-lint-ignore require-await18unitTest("qualified-path - basic", async () => {19const paths: PathInfo = {20currentFileDir: "/tmp/project/dir",21projectRoot: "/tmp/project",22};2324const projectRelative = makePath("/dir/file1", paths);25const relative = makePath("file1", paths);26const absolute = makePath("/tmp/project/dir/file1", paths, true);2728const expectedRelative = "file1";29const expectedProjectRelative = "/dir/file1";30const expectedAbsolute = "/tmp/project/dir/file1";3132for (let path of [projectRelative, relative, absolute]) {33for (let i = 0; i < 30; ++i) {34const choices = [35(path: QualifiedPath) => path.asAbsolute(paths),36(path: QualifiedPath) => path.asRelative(paths),37(path: QualifiedPath) => path.asProjectRelative(paths),38];39path = choices[~~(Math.random() * choices.length)](path);40assertEquals(path.asAbsolute(paths).value, expectedAbsolute);41assertEquals(path.asRelative(paths).value, expectedRelative);42assertEquals(43path.asProjectRelative(paths).value,44expectedProjectRelative,45);46}47}48}, {49ignore: isWindows,50});5152unitTest("qualified-path - validation", async () => {53const paths: PathInfo = {54currentFileDir: "/tmp/project/dir",55projectRoot: "/tmp/project",56};5758makePath("../file1", paths);5960//deno-lint-ignore require-await61await assertRejects(async () => {62// this should raise because it resolves outside of projectRoot.63return makePath("../../file1", paths);64});65}, {66ignore: isWindows,67});686970