1
1
import { beforeEach , afterAll , test , expect } from "@jest/globals" ;
2
2
import * as path from "path" ;
3
- import * as fs from "fs" ;
4
- import { remove , ensureDir } from "fs-extra" ;
3
+ import { remove , ensureDir , writeFile , pathExists } from "fs-extra" ;
5
4
6
5
import { RollingCache } from "../src/rollingcache" ;
7
6
8
7
9
- const defaultTestFileShape = {
10
- a : 1 ,
11
- b : 2 ,
12
- c : 3 ,
13
- } ;
14
8
const local = ( x : string ) => path . resolve ( __dirname , x ) ;
9
+ const testDir = local ( "__temp/rollingcache" ) ;
10
+ const oldCacheDir = `${ testDir } /cache` ;
11
+ const newCacheDir = `${ testDir } /cache_` ;
12
+ const testFile = "file.json" ;
13
+ const oldTestFile = `${ oldCacheDir } /${ testFile } ` ;
15
14
16
- beforeEach ( ( ) => {
17
- // pre-create oldCacheRoot
18
- return ensureDir ( local ( "fixtures/cache" ) ) . then ( ( ) => {
19
- // and newCacheRoot
20
- return ensureDir ( local ( "fixtures/cache_" ) ) . then ( ( ) => {
21
- const testfile = local ( "fixtures/cache/testfile.json" ) ;
22
- fs . writeFileSync ( testfile , JSON . stringify ( defaultTestFileShape ) , "utf8" ) ;
23
- } ) ;
24
- } ) ;
15
+ const nonExistentFile = "this-does-not-exist.json" ;
16
+ const emptyCacheRoot = `${ testDir } /empty-cache-root` ;
17
+
18
+ const testFileShape = { a : 1 , b : 2 , c : 3 } ;
19
+
20
+
21
+ beforeEach ( async ( ) => {
22
+ await ensureDir ( oldCacheDir ) ;
23
+ await ensureDir ( newCacheDir ) ;
24
+ await writeFile ( oldTestFile , JSON . stringify ( testFileShape ) , "utf8" ) ;
25
25
} ) ;
26
+ afterAll ( ( ) => remove ( testDir ) ) ;
26
27
27
- afterAll (
28
- ( ) => Promise . all ( [
29
- remove ( local ( "fixtures" ) ) ,
30
- remove ( local ( "not-real" ) ) ,
31
- ] ) ,
32
- ) ;
33
28
34
- test ( "RollingCache" , ( ) => {
29
+ test ( "RollingCache" , async ( ) => {
35
30
expect ( RollingCache ) . toBeTruthy ( ) ;
36
31
37
- const cacheRoot = local ( "fixtures" ) ;
38
- const cache = new RollingCache ( cacheRoot , true ) ;
32
+ const cache = new RollingCache ( testDir , true ) ;
39
33
expect ( Object . keys ( cache ) ) . toEqual ( [
40
34
"cacheRoot" ,
41
35
"checkNewCache" ,
@@ -44,35 +38,30 @@ test("RollingCache", () => {
44
38
"newCacheRoot" ,
45
39
] ) ;
46
40
47
- expect ( cache . exists ( "fake-file.json" ) ) . toBeFalsy ( ) ;
48
- expect ( cache . exists ( "testfile.json" ) ) . toBeTruthy ( ) ;
49
- expect ( cache . path ( "x" ) ) . toEqual ( local ( "fixtures/cache/x" ) ) ;
50
- expect ( cache . match ( [ "fake-file.json" ] ) ) . toBeFalsy ( ) ;
51
- expect ( cache . match ( [ "testfile.json" ] ) ) . toBeTruthy ( ) ;
52
- expect ( cache . read ( "testfile.json" ) ) . toEqual ( defaultTestFileShape ) ;
41
+ expect ( cache . exists ( nonExistentFile ) ) . toBeFalsy ( ) ;
42
+ expect ( cache . exists ( testFile ) ) . toBeTruthy ( ) ;
43
+ expect ( cache . path ( "x" ) ) . toEqual ( ` ${ oldCacheDir } /x` ) ;
44
+ expect ( cache . match ( [ nonExistentFile ] ) ) . toBeFalsy ( ) ;
45
+ expect ( cache . match ( [ testFile ] ) ) . toBeTruthy ( ) ;
46
+ expect ( cache . read ( testFile ) ) . toEqual ( testFileShape ) ;
53
47
54
48
cache . write ( "write-test.json" , { a : 2 , b : 2 , c : 2 } ) ;
55
- expect (
56
- cache . read ( "write-test.json" ) ,
57
- ) . toEqual ( { a : 2 , b : 2 , c : 2 } ) ;
49
+ expect ( cache . read ( "write-test.json" ) ) . toEqual ( { a : 2 , b : 2 , c : 2 } ) ;
58
50
59
51
cache . write ( "write-fail.json" , ( undefined as any ) ) ;
60
- expect (
61
- cache . read ( "write-fail.json" ) ,
62
- ) . toBeFalsy ( ) ;
52
+ expect ( cache . read ( "write-fail.json" ) ) . toBeFalsy ( ) ;
63
53
64
54
cache . touch ( "touched.json" ) ;
65
- expect ( fs . existsSync ( local ( "fixtures/cache_/ touched.json" ) ) ) . toBeTruthy ( ) ;
55
+ expect ( await pathExists ( ` ${ newCacheDir } / touched.json` ) ) . toBeTruthy ( ) ;
66
56
expect ( ( cache as any ) . rolled ) . toBeFalsy ( ) ;
67
57
68
58
cache . roll ( ) ;
69
59
expect ( ( cache as any ) . rolled ) . toBeTruthy ( ) ;
70
- expect ( fs . existsSync ( local ( "fixtures/cache_" ) ) ) . toBeFalsy ( ) ;
60
+ expect ( await pathExists ( newCacheDir ) ) . toBeFalsy ( ) ;
71
61
} ) ;
72
62
73
- test ( "RollingCache, rolled" , ( ) => {
74
- const cacheRoot = local ( "fixtures" ) ;
75
- const cache = new RollingCache ( cacheRoot , true ) ;
63
+ test ( "RollingCache, rolled" , async ( ) => {
64
+ const cache = new RollingCache ( testDir , true ) ;
76
65
// roll the cache
77
66
cache . roll ( ) ;
78
67
// rolling again hits coverage for this.rolled being true already
@@ -81,28 +70,23 @@ test("RollingCache, rolled", () => {
81
70
expect ( cache . match ( [ ] ) ) . toBeFalsy ( ) ;
82
71
83
72
cache . write ( "whatever.json" , { whatever : true } ) ;
84
- expect ( fs . existsSync ( local ( "fixtures/cache/ whatever.json" ) ) ) . toBeFalsy ( ) ;
73
+ expect ( await pathExists ( ` ${ oldCacheDir } / whatever.json` ) ) . toBeFalsy ( ) ;
85
74
86
75
cache . touch ( "touched.json" ) ;
87
- expect ( fs . existsSync ( local ( "fixtures/cache/ touched.json" ) ) ) . toBeFalsy ( ) ;
76
+ expect ( await pathExists ( ` ${ oldCacheDir } / touched.json` ) ) . toBeFalsy ( ) ;
88
77
} ) ;
89
78
90
- test ( "RollingCache, test checkNewCache" , ( done ) => {
91
- const cacheRoot = local ( "fixtures" ) ;
92
- const cache = new RollingCache ( cacheRoot , true ) ;
79
+ test ( "RollingCache, test checkNewCache" , async ( ) => {
80
+ const cache = new RollingCache ( testDir , true ) ;
93
81
94
- const preExistingTestFile = local ( "fixtures/cache_/pre-existing.json" ) ;
95
- fs . writeFile ( preExistingTestFile , JSON . stringify ( { } ) , "utf8" , ( e ?: Error ) => {
96
- if ( e ) console . log ( e ) ;
97
- expect ( cache . exists ( "pre-existing.json" ) ) . toBeTruthy ( ) ;
98
- done ( ) ;
99
- } ) ;
82
+ const preExistingFile = `${ newCacheDir } /pre-existing.json` ;
83
+ await writeFile ( preExistingFile , JSON . stringify ( { } ) ) ;
84
+ expect ( cache . exists ( "pre-existing.json" ) ) . toBeTruthy ( ) ;
100
85
} ) ;
101
86
102
- test ( "RollingCache, test match when oldCacheRoot is empty" , ( ) => {
103
- const cacheRoot = local ( "not-real" ) ;
104
- const cache = new RollingCache ( cacheRoot , true ) ;
87
+ test ( "RollingCache, test match when oldCacheDir is empty" , ( ) => {
88
+ const cache = new RollingCache ( emptyCacheRoot , true ) ;
105
89
106
90
expect ( cache . match ( [ ] ) ) . toBeTruthy ( ) ;
107
- expect ( cache . match ( [ "file.json" ] ) ) . toBeFalsy ( ) ;
91
+ expect ( cache . match ( [ testFile ] ) ) . toBeFalsy ( ) ;
108
92
} ) ;
0 commit comments