-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
test.js
57 lines (45 loc) · 1.97 KB
/
test.js
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
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import {deleteSync} from 'del';
import {temporaryDirectory} from 'tempy';
import findCacheDirectory from './index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
test('finds from a list of files', t => {
process.chdir(path.join(__dirname, '..'));
const files = ['foo/bar', 'baz/quz'].map(file => path.join(__dirname, file));
t.is(findCacheDirectory({files, name: 'blah'}), path.join(__dirname, 'node_modules', '.cache', 'blah'));
});
test('finds from process.cwd', t => {
process.chdir(path.join(__dirname));
t.is(findCacheDirectory({name: 'foo'}), path.join(__dirname, 'node_modules', '.cache', 'foo'));
});
test('finds from options.cwd', t => {
process.chdir(path.join(__dirname, '..'));
t.is(findCacheDirectory({cwd: __dirname, name: 'bar'}), path.join(__dirname, 'node_modules', '.cache', 'bar'));
});
test('creates directory', t => {
const directory = path.join(__dirname, 'node_modules', '.cache', 'created');
deleteSync(directory);
findCacheDirectory({create: true, name: 'created', cwd: __dirname});
t.true(fs.existsSync(directory));
});
test('returns undefined if it can\'t find package.json', t => {
process.chdir(path.join(__dirname, '..'));
t.is(findCacheDirectory({name: 'foo'}), undefined);
});
test('supports CACHE_DIR environment variable', t => {
const newCacheDirectory = temporaryDirectory();
const finalDirectory = path.join(newCacheDirectory, 'some-package');
process.env.CACHE_DIR = newCacheDirectory;
t.is(findCacheDirectory({name: 'some-package'}), finalDirectory);
findCacheDirectory({name: 'some-package', create: true});
t.true(fs.existsSync(finalDirectory));
delete process.env.CACHE_DIR;
});
test('ignores `false` for CACHE_DIR environment variable', t => {
process.env.CACHE_DIR = 'false';
t.not(findCacheDirectory(), path.resolve(__dirname, 'false', 'find-cache-dir'));
});