Skip to content

Commit 9af2112

Browse files
committed
chore: update CHANGELOG
1 parent 1bf52f5 commit 9af2112

File tree

4 files changed

+173
-30
lines changed

4 files changed

+173
-30
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ VS Code v1.56
7070

7171
### Development
7272

73-
- item
73+
- fix(socket): use xdgBasedir.runtime instead of tmp #3304 @jsjoeio
7474

7575
## 3.10.0
7676

src/node/socket.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class SocketProxyProvider {
7676
.then((pipe) => {
7777
this.proxyPipe = pipe
7878
return Promise.all([
79-
fs.mkdir(paths.runtime, { recursive: true }),
79+
fs.mkdir(path.dirname(this.proxyPipe), { recursive: true }),
8080
fs.rmdir(this.proxyPipe, { recursive: true }),
8181
])
8282
})

src/node/util.ts

+27-21
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import * as os from "os"
77
import * as path from "path"
88
import * as util from "util"
99
import xdgBasedir from "xdg-basedir"
10-
import { tmpdir } from "./constants"
1110

12-
interface Paths {
11+
export interface Paths {
1312
data: string
1413
config: string
1514
runtime: string
@@ -23,26 +22,33 @@ export const paths = getEnvPaths()
2322
* ones. Most CLIs do this as in practice only GUI apps use the standard macOS directories.
2423
*/
2524
export function getEnvPaths(): Paths {
26-
let paths: Paths
27-
if (process.platform === "win32") {
28-
paths = {
29-
...envPaths("code-server", {
30-
suffix: "",
31-
}),
32-
runtime: tmpdir,
33-
}
34-
} else {
35-
if (xdgBasedir.data === undefined || xdgBasedir.config === undefined) {
36-
throw new Error("No home folder?")
37-
}
38-
paths = {
39-
data: path.join(xdgBasedir.data, "code-server"),
40-
config: path.join(xdgBasedir.config, "code-server"),
41-
runtime: xdgBasedir.runtime ? path.join(xdgBasedir.runtime, "code-server") : tmpdir,
42-
}
25+
const paths = envPaths("code-server", { suffix: "" })
26+
const append = (p: string): string => path.join(p, "code-server")
27+
switch (process.platform) {
28+
case "darwin":
29+
return {
30+
// envPaths uses native directories so force Darwin to use the XDG spec
31+
// to align with other CLI tools.
32+
data: xdgBasedir.data ? append(xdgBasedir.data) : paths.data,
33+
config: xdgBasedir.config ? append(xdgBasedir.config) : paths.config,
34+
// Fall back to temp if there is no runtime dir.
35+
runtime: xdgBasedir.runtime ? append(xdgBasedir.runtime) : paths.temp,
36+
}
37+
case "win32":
38+
return {
39+
data: paths.data,
40+
config: paths.config,
41+
// Windows doesn't have a runtime dir.
42+
runtime: paths.temp,
43+
}
44+
default:
45+
return {
46+
data: paths.data,
47+
config: paths.config,
48+
// Fall back to temp if there is no runtime dir.
49+
runtime: xdgBasedir.runtime ? append(xdgBasedir.runtime) : paths.temp,
50+
}
4351
}
44-
45-
return paths
4652
}
4753

4854
/**

test/unit/node/util.test.ts

+144-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,147 @@
1-
import { getEnvPaths } from "../../../src/node/util"
2-
31
describe("getEnvPaths", () => {
4-
it("should return an object with the data, config and runtime path", () => {
5-
const actualPaths = getEnvPaths()
6-
expect(actualPaths.hasOwnProperty("data")).toBe(true)
7-
expect(actualPaths.hasOwnProperty("config")).toBe(true)
8-
expect(actualPaths.hasOwnProperty("runtime")).toBe(true)
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+
})
9146
})
10147
})

0 commit comments

Comments
 (0)