-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappstash.test.ts
More file actions
205 lines (157 loc) · 7.14 KB
/
appstash.test.ts
File metadata and controls
205 lines (157 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { appstash, ensure, resolve } from '../src';
describe('appstash', () => {
let tempBase: string;
beforeEach(() => {
tempBase = fs.mkdtempSync(path.join(os.tmpdir(), 'appstash-test-'));
});
afterEach(() => {
if (fs.existsSync(tempBase)) {
fs.rmSync(tempBase, { recursive: true, force: true });
}
});
describe('Basic functionality', () => {
it('should return correct directory paths for a tool', () => {
const dirs = appstash('pgpm', { baseDir: tempBase });
expect(dirs.root).toBe(path.join(tempBase, '.pgpm'));
expect(dirs.config).toBe(path.join(tempBase, '.pgpm', 'config'));
expect(dirs.cache).toBe(path.join(tempBase, '.pgpm', 'cache'));
expect(dirs.data).toBe(path.join(tempBase, '.pgpm', 'data'));
expect(dirs.logs).toBe(path.join(tempBase, '.pgpm', 'logs'));
expect(dirs.tmp).toBe(path.join(os.tmpdir(), 'pgpm'));
expect(dirs.usedFallback).toBeUndefined();
});
it('should work with different tool names', () => {
const dirs1 = appstash('lql', { baseDir: tempBase });
const dirs2 = appstash('myapp', { baseDir: tempBase });
expect(dirs1.root).toBe(path.join(tempBase, '.lql'));
expect(dirs2.root).toBe(path.join(tempBase, '.myapp'));
});
it('should use custom tmpRoot when provided', () => {
const customTmp = path.join(tempBase, 'tmp');
const dirs = appstash('pgpm', { baseDir: tempBase, tmpRoot: customTmp });
expect(dirs.tmp).toBe(path.join(customTmp, 'pgpm'));
});
});
describe('ensure option', () => {
it('should create directories when ensure is true', () => {
const dirs = appstash('pgpm', { baseDir: tempBase, ensure: true });
expect(fs.existsSync(dirs.root)).toBe(true);
expect(fs.existsSync(dirs.config)).toBe(true);
expect(fs.existsSync(dirs.cache)).toBe(true);
expect(fs.existsSync(dirs.data)).toBe(true);
expect(fs.existsSync(dirs.logs)).toBe(true);
expect(dirs.usedFallback).toBeFalsy();
});
it('should not create directories when ensure is false', () => {
const dirs = appstash('pgpm', { baseDir: tempBase, ensure: false });
expect(fs.existsSync(dirs.root)).toBe(false);
expect(fs.existsSync(dirs.config)).toBe(false);
});
it('should not create tmp directory', () => {
const customTmp = path.join(tempBase, 'tmp');
const dirs = appstash('pgpm', { baseDir: tempBase, ensure: true, tmpRoot: customTmp });
expect(fs.existsSync(dirs.tmp)).toBe(false);
});
});
describe('ensure function', () => {
it('should create directories that do not exist', () => {
const dirs = appstash('pgpm', { baseDir: tempBase });
const result = ensure(dirs);
expect(result.created.length).toBeGreaterThan(0);
expect(result.created).toContain(dirs.root);
expect(result.created).toContain(dirs.config);
expect(result.usedFallback).toBe(false);
expect(fs.existsSync(dirs.root)).toBe(true);
expect(fs.existsSync(dirs.config)).toBe(true);
});
it('should not recreate existing directories', () => {
const dirs = appstash('pgpm', { baseDir: tempBase, ensure: true });
const result = ensure(dirs);
expect(result.created.length).toBe(0);
expect(result.usedFallback).toBe(false);
});
});
describe('resolve function', () => {
it('should resolve paths within config directory', () => {
const dirs = appstash('pgpm', { baseDir: tempBase });
const configPath = resolve(dirs, 'config', 'settings.json');
expect(configPath).toBe(path.join(dirs.config, 'settings.json'));
});
it('should resolve paths within cache directory', () => {
const dirs = appstash('pgpm', { baseDir: tempBase });
const cachePath = resolve(dirs, 'cache', 'repos', 'my-repo');
expect(cachePath).toBe(path.join(dirs.cache, 'repos', 'my-repo'));
});
it('should resolve paths within data directory', () => {
const dirs = appstash('pgpm', { baseDir: tempBase });
const dataPath = resolve(dirs, 'data', 'db.json');
expect(dataPath).toBe(path.join(dirs.data, 'db.json'));
});
it('should resolve paths within logs directory', () => {
const dirs = appstash('pgpm', { baseDir: tempBase });
const logPath = resolve(dirs, 'logs', 'app.log');
expect(logPath).toBe(path.join(dirs.logs, 'app.log'));
});
it('should resolve paths within tmp directory', () => {
const dirs = appstash('pgpm', { baseDir: tempBase });
const tmpPath = resolve(dirs, 'tmp', 'temp-file.txt');
expect(tmpPath).toBe(path.join(dirs.tmp, 'temp-file.txt'));
});
});
describe('APPSTASH_BASE_DIR env var', () => {
const originalEnv = process.env.APPSTASH_BASE_DIR;
afterEach(() => {
if (originalEnv === undefined) {
delete process.env.APPSTASH_BASE_DIR;
} else {
process.env.APPSTASH_BASE_DIR = originalEnv;
}
});
it('should use APPSTASH_BASE_DIR when no baseDir option is provided', () => {
process.env.APPSTASH_BASE_DIR = tempBase;
const dirs = appstash('pgpm');
expect(dirs.root).toBe(path.join(tempBase, '.pgpm'));
expect(dirs.config).toBe(path.join(tempBase, '.pgpm', 'config'));
});
it('should prefer baseDir option over APPSTASH_BASE_DIR env var', () => {
const otherBase = fs.mkdtempSync(path.join(os.tmpdir(), 'appstash-other-'));
process.env.APPSTASH_BASE_DIR = otherBase;
const dirs = appstash('pgpm', { baseDir: tempBase });
expect(dirs.root).toBe(path.join(tempBase, '.pgpm'));
fs.rmSync(otherBase, { recursive: true, force: true });
});
});
describe('Edge cases', () => {
it('should handle tool names with special characters', () => {
const dirs = appstash('my-app', { baseDir: tempBase });
expect(dirs.root).toBe(path.join(tempBase, '.my-app'));
});
it('should be consistent across multiple calls', () => {
const dirs1 = appstash('pgpm', { baseDir: tempBase });
const dirs2 = appstash('pgpm', { baseDir: tempBase });
expect(dirs1.root).toBe(dirs2.root);
expect(dirs1.config).toBe(dirs2.config);
expect(dirs1.cache).toBe(dirs2.cache);
});
});
describe('Integration tests', () => {
it('should support full workflow: resolve, ensure, use', () => {
const dirs = appstash('pgpm', { baseDir: tempBase });
const ensureResult = ensure(dirs);
expect(ensureResult.usedFallback).toBe(false);
const configFile = resolve(dirs, 'config', 'settings.json');
fs.writeFileSync(configFile, JSON.stringify({ test: 'value' }));
const content = JSON.parse(fs.readFileSync(configFile, 'utf8'));
expect(content.test).toBe('value');
});
it('should work with ensure option', () => {
const dirs = appstash('pgpm', { baseDir: tempBase, ensure: true });
expect(fs.existsSync(dirs.root)).toBe(true);
expect(fs.existsSync(dirs.config)).toBe(true);
expect(dirs.root).toBe(path.join(tempBase, '.pgpm'));
});
});
});