-
Notifications
You must be signed in to change notification settings - Fork 565
Expand file tree
/
Copy pathclean.js
More file actions
122 lines (100 loc) · 3.05 KB
/
clean.js
File metadata and controls
122 lines (100 loc) · 3.05 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
import fs from 'fs';
import * as path from 'path';
import { v4 as uuidv4} from 'uuid';
export {
before,
after,
afterEach
};
// Unique id for each 'run' of the entire test suite
let testSuiteRunId = uuidv4();
// At the beginning of a run purge .tmp
function before (tmpPath) {
cleanPath(tmpPath);
// clean(tmpPath, 'all');
}
function afterEach (tmpPath, preserve) {
if (preserve !== false) {
archive(tmpPath, testSuiteRunId);
}
cleanPath(tmpPath);
}
// After should listen to the user via the config
// Before should always purge .tmp irregardless of config
function after (tmpPath, preserve) {
clean(tmpPath, preserve);
}
/**
* Copies the .tmp folder to the artifacts folder,
* then clears the .tmp folder
*
* Generally should be run in afterEach()
*/
function archive (tmpPath, testSuiteRunId) {
let destinationPath = path.resolve(tmpPath + '/../artifacts/' + testSuiteRunId + '/' + uuidv4());
fs.mkdirSync(destinationPath, { recursive: true });
fs.cpSync(tmpPath, destinationPath, { recursive: true });
}
/**
* Cleans up the artifacts folder
*
* Generally called in after()
*/
function clean (tmpPath, preserve) {
/**
* If preserve is a normal integer over 0 thats how many results to keep.
* If string 'all' then keep all
* Else: don't preserve anything
*/
if (preserve === 'all') {
/**
* Preserve all artifacts
* Don't purge any artifacts
*
* BEWARE: this can fill the disk up over time
*/
return;
} else if (isNormalNonZeroInteger(preserve)) {
/**
* Preserve a specific number of artifacts
*
* 1 = keep only th last run
* 2 = keep the last run and the one before it, purging any that happened beforehand
*/
// Set the path
let artifactsBasePath = path.resolve(tmpPath + '/../artifacts');
// The the files in this path
let artifactFolders = fs.readdirSync(artifactsBasePath);
// Reverse chronologially sort the files
artifactFolders.sort(function (a, b) {
return fs.statSync(path.resolve(artifactsBasePath, b)).mtime.getTime() - fs.statSync(path.resolve(artifactsBasePath, a)).mtime.getTime();
});
// Keep only the number of files defined in the config setting 'preserve'.
keep(artifactsBasePath, artifactFolders, preserve);
}
// Always purge tmp, it needs to be empty for next run
cleanPath(tmpPath);
}
function isNormalNonZeroInteger (str) {
if (Number.isInteger(str) && str > 0) { // Check for integers above 0
return str;
} else { // Check for strings that cast to ints that are above 0
var n = ~~Number(str);
return String(n) === str && n > 0;
}
}
/**
* Given a reverse chronological array of paths
* This deletes all files except for n of them
*
* n is the (1 indexed) count of files to keep.
*/
function keep (basePath, paths, n) {
for (let i = paths.length; i > n; i--) {
fs.rmSync(path.resolve(basePath, paths[i - 1]), { recursive: true, force: true });
}
}
function cleanPath (tmpPath) {
fs.rmSync(tmpPath, { recursive: true, force: true });
fs.mkdirSync(tmpPath, { recursive: true });
}