|
| 1 | +describe("getEnvPaths", () => { |
| 2 | + describe("on darwin", () => { |
| 3 | + let ORIGINAL_PLATFORM = "" |
| 4 | + |
| 5 | + beforeAll(() => { |
| 6 | + ORIGINAL_PLATFORM = process.platform |
| 7 | + |
| 8 | + Object.defineProperty(process, "platform", { |
| 9 | + value: "darwin", |
| 10 | + }) |
| 11 | + }) |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + jest.resetModules() |
| 15 | + jest.mock("env-paths", () => { |
| 16 | + return () => ({ |
| 17 | + data: "/home/envPath/.local/share", |
| 18 | + config: "/home/envPath/.config", |
| 19 | + temp: "/tmp/envPath/runtime", |
| 20 | + }) |
| 21 | + }) |
| 22 | + }) |
| 23 | + |
| 24 | + afterAll(() => { |
| 25 | + // Restore old platform |
| 26 | + |
| 27 | + Object.defineProperty(process, "platform", { |
| 28 | + value: ORIGINAL_PLATFORM, |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + it("should return the env paths using xdgBasedir", () => { |
| 33 | + jest.mock("xdg-basedir", () => ({ |
| 34 | + data: "/home/usr/.local/share", |
| 35 | + config: "/home/usr/.config", |
| 36 | + runtime: "/tmp/runtime", |
| 37 | + })) |
| 38 | + const getEnvPaths = require("../../../src/node/util").getEnvPaths |
| 39 | + const envPaths = getEnvPaths() |
| 40 | + |
| 41 | + expect(envPaths.data).toEqual("/home/usr/.local/share/code-server") |
| 42 | + expect(envPaths.config).toEqual("/home/usr/.config/code-server") |
| 43 | + expect(envPaths.runtime).toEqual("/tmp/runtime/code-server") |
| 44 | + }) |
| 45 | + |
| 46 | + it("should return the env paths using envPaths when xdgBasedir is undefined", () => { |
| 47 | + jest.mock("xdg-basedir", () => ({})) |
| 48 | + const getEnvPaths = require("../../../src/node/util").getEnvPaths |
| 49 | + const envPaths = getEnvPaths() |
| 50 | + |
| 51 | + expect(envPaths.data).toEqual("/home/envPath/.local/share") |
| 52 | + expect(envPaths.config).toEqual("/home/envPath/.config") |
| 53 | + expect(envPaths.runtime).toEqual("/tmp/envPath/runtime") |
| 54 | + }) |
| 55 | + }) |
| 56 | + describe("on win32", () => { |
| 57 | + let ORIGINAL_PLATFORM = "" |
| 58 | + |
| 59 | + beforeAll(() => { |
| 60 | + ORIGINAL_PLATFORM = process.platform |
| 61 | + |
| 62 | + Object.defineProperty(process, "platform", { |
| 63 | + value: "win32", |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + beforeEach(() => { |
| 68 | + jest.resetModules() |
| 69 | + jest.mock("env-paths", () => { |
| 70 | + return () => ({ |
| 71 | + data: "/windows/envPath/.local/share", |
| 72 | + config: "/windows/envPath/.config", |
| 73 | + temp: "/tmp/envPath/runtime", |
| 74 | + }) |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + afterAll(() => { |
| 79 | + // Restore old platform |
| 80 | + |
| 81 | + Object.defineProperty(process, "platform", { |
| 82 | + value: ORIGINAL_PLATFORM, |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + it("should return the env paths using envPaths", () => { |
| 87 | + const getEnvPaths = require("../../../src/node/util").getEnvPaths |
| 88 | + const envPaths = getEnvPaths() |
| 89 | + |
| 90 | + expect(envPaths.data).toEqual("/windows/envPath/.local/share") |
| 91 | + expect(envPaths.config).toEqual("/windows/envPath/.config") |
| 92 | + expect(envPaths.runtime).toEqual("/tmp/envPath/runtime") |
| 93 | + }) |
| 94 | + }) |
| 95 | + describe("on other platforms", () => { |
| 96 | + let ORIGINAL_PLATFORM = "" |
| 97 | + |
| 98 | + beforeAll(() => { |
| 99 | + ORIGINAL_PLATFORM = process.platform |
| 100 | + |
| 101 | + Object.defineProperty(process, "platform", { |
| 102 | + value: "linux", |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + beforeEach(() => { |
| 107 | + jest.resetModules() |
| 108 | + jest.mock("env-paths", () => { |
| 109 | + return () => ({ |
| 110 | + data: "/linux/envPath/.local/share", |
| 111 | + config: "/linux/envPath/.config", |
| 112 | + temp: "/tmp/envPath/runtime", |
| 113 | + }) |
| 114 | + }) |
| 115 | + }) |
| 116 | + |
| 117 | + afterAll(() => { |
| 118 | + // Restore old platform |
| 119 | + |
| 120 | + Object.defineProperty(process, "platform", { |
| 121 | + value: ORIGINAL_PLATFORM, |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
| 125 | + it("should return the runtime using xdgBasedir if it exists", () => { |
| 126 | + jest.mock("xdg-basedir", () => ({ |
| 127 | + runtime: "/tmp/runtime", |
| 128 | + })) |
| 129 | + const getEnvPaths = require("../../../src/node/util").getEnvPaths |
| 130 | + const envPaths = getEnvPaths() |
| 131 | + |
| 132 | + expect(envPaths.data).toEqual("/linux/envPath/.local/share") |
| 133 | + expect(envPaths.config).toEqual("/linux/envPath/.config") |
| 134 | + expect(envPaths.runtime).toEqual("/tmp/runtime/code-server") |
| 135 | + }) |
| 136 | + |
| 137 | + it("should return the env paths using envPaths when xdgBasedir is undefined", () => { |
| 138 | + jest.mock("xdg-basedir", () => ({})) |
| 139 | + const getEnvPaths = require("../../../src/node/util").getEnvPaths |
| 140 | + const envPaths = getEnvPaths() |
| 141 | + |
| 142 | + expect(envPaths.data).toEqual("/linux/envPath/.local/share") |
| 143 | + expect(envPaths.config).toEqual("/linux/envPath/.config") |
| 144 | + expect(envPaths.runtime).toEqual("/tmp/envPath/runtime") |
| 145 | + }) |
| 146 | + }) |
| 147 | +}) |
0 commit comments