|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { describe, expect, it } from "@effect/vitest"; |
| 3 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as FileSystem from "effect/FileSystem"; |
| 6 | +import * as Option from "effect/Option"; |
| 7 | +import * as Path from "effect/Path"; |
| 8 | + |
| 9 | +import { parseXdgProjectsDir, resolveUserProjectsRoot } from "./userProjectsRoot.ts"; |
| 10 | + |
| 11 | +// Written by xdg-user-dirs-update 0.20 for a new account |
| 12 | +const GENERATED_USER_DIRS = `# This file is written by xdg-user-dirs-update |
| 13 | +# If you want to change or add directories, just edit the line you're |
| 14 | +# interested in. All local changes will be retained on the next run. |
| 15 | +# Format is XDG_xxx_DIR="$HOME/yyy", where yyy is a shell-escaped |
| 16 | +# homedir-relative path, or XDG_xxx_DIR="/yyy", where /yyy is an |
| 17 | +# absolute path. No other format is supported. |
| 18 | +# |
| 19 | +XDG_DESKTOP_DIR="$HOME/Desktop" |
| 20 | +XDG_DOWNLOAD_DIR="$HOME/Downloads" |
| 21 | +XDG_DOCUMENTS_DIR="$HOME/Documents" |
| 22 | +XDG_PROJECTS_DIR="$HOME/Projects" |
| 23 | +`; |
| 24 | + |
| 25 | +describe("parseXdgProjectsDir", () => { |
| 26 | + it("reads the directory xdg-user-dirs-update writes", () => { |
| 27 | + expect(parseXdgProjectsDir(GENERATED_USER_DIRS)).toEqual(Option.some("~/Projects")); |
| 28 | + }); |
| 29 | + |
| 30 | + it("accepts the same values as xdg-user-dir", () => { |
| 31 | + const cases: ReadonlyArray<readonly [string, string]> = [ |
| 32 | + ['XDG_PROJECTS_DIR="/srv/code/"', "/srv/code"], |
| 33 | + ['XDG_PROJECTS_DIR="$HOME/My Projects"', "~/My Projects"], |
| 34 | + ['XDG_PROJECTS_DIR="$HOME/a\\"b"', '~/a"b'], |
| 35 | + ['XDG_PROJECTS_DIR="$HOME/a\\\\b"', "~/a\\b"], |
| 36 | + ['XDG_PROJECTS_DIR="$HOME/a\\$b"', "~/a$b"], |
| 37 | + ['\t XDG_PROJECTS_DIR \t= \t"$HOME/code" trailing', "~/code"], |
| 38 | + ['XDG_PROJECTS_DIR="$HOME/code', "~/code"], |
| 39 | + ['XDG_PROJECTS_DIR="$HOME/"', "~"], |
| 40 | + ['XDG_PROJECTS_DIR="/"', "/"], |
| 41 | + ]; |
| 42 | + for (const [line, root] of cases) { |
| 43 | + expect(parseXdgProjectsDir(line), line).toEqual(Option.some(root)); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + it("ignores lines xdg-user-dir ignores", () => { |
| 48 | + const lines = [ |
| 49 | + "XDG_PROJECTS_DIR=$HOME/unquoted", |
| 50 | + 'export XDG_PROJECTS_DIR="$HOME/exported"', |
| 51 | + 'XDG_PROJECTS_DIR="${HOME}/braced"', |
| 52 | + 'XDG_PROJECTS_DIR="$HOMEx/code"', |
| 53 | + 'XDG_PROJECTS_DIR="relative/code"', |
| 54 | + 'XDG_PROJECTS_DIR=""', |
| 55 | + '#XDG_PROJECTS_DIR="$HOME/commented"', |
| 56 | + 'XDG_PROJECTS_DIRS="$HOME/other-key"', |
| 57 | + ]; |
| 58 | + for (const line of lines) { |
| 59 | + expect(parseXdgProjectsDir(line), line).toEqual(Option.none()); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + it("uses the last valid line", () => { |
| 64 | + expect( |
| 65 | + parseXdgProjectsDir( |
| 66 | + 'XDG_PROJECTS_DIR="$HOME/first"\r\nXDG_PROJECTS_DIR="$HOME/second"\r\nXDG_PROJECTS_DIR=bad\r\n', |
| 67 | + ), |
| 68 | + ).toEqual(Option.some("~/second")); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +it.layer(NodeServices.layer)("resolveUserProjectsRoot", (it) => { |
| 73 | + const makeHome = Effect.fn("makeHome")(function* (input: { |
| 74 | + readonly userDirs?: string; |
| 75 | + readonly directories?: ReadonlyArray<string>; |
| 76 | + }) { |
| 77 | + const fileSystem = yield* FileSystem.FileSystem; |
| 78 | + const path = yield* Path.Path; |
| 79 | + const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-user-projects-" }); |
| 80 | + for (const directory of input.directories ?? []) { |
| 81 | + yield* fileSystem.makeDirectory(path.join(home, directory), { recursive: true }); |
| 82 | + } |
| 83 | + if (input.userDirs !== undefined) { |
| 84 | + yield* fileSystem.makeDirectory(path.join(home, ".config"), { recursive: true }); |
| 85 | + yield* fileSystem.writeFileString( |
| 86 | + path.join(home, ".config", "user-dirs.dirs"), |
| 87 | + input.userDirs, |
| 88 | + ); |
| 89 | + } |
| 90 | + return home; |
| 91 | + }); |
| 92 | + |
| 93 | + const resolveFor = ( |
| 94 | + home: string, |
| 95 | + options: { readonly platform?: NodeJS.Platform; readonly env?: NodeJS.ProcessEnv } = {}, |
| 96 | + ) => |
| 97 | + resolveUserProjectsRoot().pipe( |
| 98 | + Effect.provideService(HostProcess.Platform, options.platform ?? "linux"), |
| 99 | + Effect.provideService(HostProcess.HomeDirectory, home), |
| 100 | + Effect.provideService(HostProcess.Environment, options.env ?? {}), |
| 101 | + ); |
| 102 | + |
| 103 | + it.effect("returns the Projects directory when it exists", () => |
| 104 | + Effect.gen(function* () { |
| 105 | + const home = yield* makeHome({ userDirs: GENERATED_USER_DIRS, directories: ["Projects"] }); |
| 106 | + |
| 107 | + expect(yield* resolveFor(home)).toEqual(Option.some("~/Projects")); |
| 108 | + }), |
| 109 | + ); |
| 110 | + |
| 111 | + it.effect("returns none when the directory was deleted", () => |
| 112 | + Effect.gen(function* () { |
| 113 | + const home = yield* makeHome({ userDirs: GENERATED_USER_DIRS }); |
| 114 | + |
| 115 | + expect(yield* resolveFor(home)).toEqual(Option.none()); |
| 116 | + }), |
| 117 | + ); |
| 118 | + |
| 119 | + it.effect("returns none without user-dirs.dirs", () => |
| 120 | + Effect.gen(function* () { |
| 121 | + const home = yield* makeHome({ directories: ["Projects"] }); |
| 122 | + |
| 123 | + expect(yield* resolveFor(home)).toEqual(Option.none()); |
| 124 | + }), |
| 125 | + ); |
| 126 | + |
| 127 | + it.effect("returns none when the directory is disabled by pointing it at home", () => |
| 128 | + Effect.gen(function* () { |
| 129 | + const home = yield* makeHome({ userDirs: 'XDG_PROJECTS_DIR="$HOME/"\n' }); |
| 130 | + |
| 131 | + expect(yield* resolveFor(home)).toEqual(Option.none()); |
| 132 | + }), |
| 133 | + ); |
| 134 | + |
| 135 | + // user-dirs.dirs only takes POSIX absolute paths |
| 136 | + it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( |
| 137 | + "reads user-dirs.dirs from an absolute XDG_CONFIG_HOME", |
| 138 | + () => |
| 139 | + Effect.gen(function* () { |
| 140 | + const fileSystem = yield* FileSystem.FileSystem; |
| 141 | + const path = yield* Path.Path; |
| 142 | + const home = yield* makeHome({ directories: ["code", "config"] }); |
| 143 | + yield* fileSystem.writeFileString( |
| 144 | + path.join(home, "config", "user-dirs.dirs"), |
| 145 | + `XDG_PROJECTS_DIR="${path.join(home, "code")}"\n`, |
| 146 | + ); |
| 147 | + |
| 148 | + expect( |
| 149 | + yield* resolveFor(home, { env: { XDG_CONFIG_HOME: path.join(home, "config") } }), |
| 150 | + ).toEqual(Option.some(path.join(home, "code"))); |
| 151 | + expect(yield* resolveFor(home, { env: { XDG_CONFIG_HOME: "config" } })).toEqual( |
| 152 | + Option.none(), |
| 153 | + ); |
| 154 | + }), |
| 155 | + ); |
| 156 | + |
| 157 | + it.effect("returns none on Windows and macOS", () => |
| 158 | + Effect.gen(function* () { |
| 159 | + const home = yield* makeHome({ userDirs: GENERATED_USER_DIRS, directories: ["Projects"] }); |
| 160 | + |
| 161 | + expect(yield* resolveFor(home, { platform: "win32" })).toEqual(Option.none()); |
| 162 | + expect(yield* resolveFor(home, { platform: "darwin" })).toEqual(Option.none()); |
| 163 | + }), |
| 164 | + ); |
| 165 | +}); |
0 commit comments