@@ -17,96 +17,102 @@ import { createHardhatRuntimeEnvironment } from "../../../../src/hre.js";
17
17
import cleanAction from "../../../../src/internal/builtin-plugins/clean/task-action.js" ;
18
18
import { useFixtureProject } from "../../../helpers/project.js" ;
19
19
20
- function assertCleanBehavior ( global : boolean , globalCacheDir : string ) {
21
- it ( "should empty the cache dir" , async ( ) => {
22
- const cacheContents = await readdir ( path . join ( process . cwd ( ) , "cache" ) ) ;
23
- assert . ok ( cacheContents . length === 0 , "Cache dir is not empty" ) ;
24
- } ) ;
25
-
26
- it ( "should remove the artifacts dir" , async ( ) => {
27
- assert . ok (
28
- exists ( path . join ( process . cwd ( ) , "artifacts" ) ) ,
29
- "Artifacts dir does not exist" ,
30
- ) ;
31
- } ) ;
32
-
33
- if ( global ) {
34
- it ( "should empty the global cache dir when the global flag is true" , async ( ) => {
20
+ let hre : HardhatRuntimeEnvironment ;
21
+ let globalCacheDir : string ;
22
+ let cacheDir : string ;
23
+ let artifactsDir : string ;
24
+
25
+ function assertCleanBehavior ( global : boolean ) {
26
+ it ( "should clean the cache and artifacts directories" , async ( ) => {
27
+ await cleanAction ( { global } , hre ) ;
28
+
29
+ // If the cache dir exists, it should be empty
30
+ if ( await exists ( cacheDir ) ) {
31
+ const cacheContents = await readdir ( cacheDir ) ;
32
+ assert . ok ( cacheContents . length === 0 , "Cache dir is not empty" ) ;
33
+ }
34
+
35
+ // The artifacts dir should not exist
36
+ assert . ok ( ! ( await exists ( artifactsDir ) ) , "Artifacts dir exists" ) ;
37
+
38
+ // If the global cache dir exists, it should be empty if the global flag is
39
+ // true, and not empty otherwise
40
+ if ( await exists ( globalCacheDir ) ) {
35
41
const globalCacheContents = await readdir ( globalCacheDir ) ;
36
- assert . ok (
37
- globalCacheContents . length === 0 ,
38
- "Global cache dir is not empty" ,
39
- ) ;
40
- } ) ;
41
- } else {
42
- it ( "should not empty the global cache dir when the global flag is false" , async ( ) => {
43
- const globalCacheContents = await readdir ( globalCacheDir ) ;
44
- assert . ok ( globalCacheContents . length > 0 , "Global cache dir is empty" ) ;
45
- } ) ;
46
- }
42
+ if ( global ) {
43
+ assert . ok (
44
+ globalCacheContents . length === 0 ,
45
+ "Global cache dir is not empty" ,
46
+ ) ;
47
+ } else {
48
+ assert . ok (
49
+ globalCacheContents . length > 0 ,
50
+ "Global cache dir is empty when it shouldn't be" ,
51
+ ) ;
52
+ }
53
+ }
54
+ } ) ;
47
55
}
48
56
49
57
describe ( "clean/task-action" , ( ) => {
50
- let hre : HardhatRuntimeEnvironment ;
51
- let globalCacheDir : string ;
52
-
53
- before ( async function ( ) {
54
- hre = await createHardhatRuntimeEnvironment ( { } ) ;
55
- globalCacheDir = await getCacheDir ( ) ;
56
- } ) ;
57
-
58
58
describe ( "cleanAction" , ( ) => {
59
59
useFixtureProject ( "loaded-config" ) ;
60
60
61
+ before ( async function ( ) {
62
+ globalCacheDir = await getCacheDir ( ) ;
63
+ cacheDir = path . join ( process . cwd ( ) , "cache" ) ;
64
+ artifactsDir = path . join ( process . cwd ( ) , "artifacts" ) ;
65
+ hre = await createHardhatRuntimeEnvironment ( {
66
+ // TODO remove this once cache and artifacts are resolved in the config
67
+ paths : { cache : cacheDir , artifacts : artifactsDir } ,
68
+ } ) ;
69
+ } ) ;
70
+
61
71
describe ( "when cache and artifact dirs don't exist" , async ( ) => {
62
72
beforeEach ( async ( ) => {
63
73
await remove ( globalCacheDir ) ;
64
- await remove ( path . join ( process . cwd ( ) , "cache" ) ) ;
65
- await remove ( path . join ( process . cwd ( ) , "artifacts" ) ) ;
74
+ await remove ( cacheDir ) ;
75
+ await remove ( artifactsDir ) ;
66
76
} ) ;
67
77
68
- await cleanAction ( { global : true } , hre ) ;
69
- assertCleanBehavior ( true , globalCacheDir ) ;
78
+ assertCleanBehavior ( true ) ;
70
79
} ) ;
71
80
72
81
describe ( "when cache and artifact are empty dirs" , async ( ) => {
73
82
beforeEach ( async ( ) => {
74
83
await remove ( globalCacheDir ) ;
75
- await remove ( path . join ( process . cwd ( ) , "cache" ) ) ;
76
- await remove ( path . join ( process . cwd ( ) , "artifacts" ) ) ;
77
- await getCacheDir ( ) ; // Recreate the cache dir
78
- await mkdir ( path . join ( process . cwd ( ) , "cache" ) ) ;
79
- await mkdir ( path . join ( process . cwd ( ) , "artifacts" ) ) ;
84
+ await remove ( cacheDir ) ;
85
+ await remove ( artifactsDir ) ;
86
+ await getCacheDir ( ) ; // Calling this recreates the cache dir
87
+ await mkdir ( cacheDir ) ;
88
+ await mkdir ( artifactsDir ) ;
80
89
} ) ;
81
90
82
- await cleanAction ( { global : true } , hre ) ;
83
- assertCleanBehavior ( true , globalCacheDir ) ;
91
+ assertCleanBehavior ( true ) ;
84
92
} ) ;
85
93
86
94
describe ( "when cache and artifact dirs aren't empty" , async ( ) => {
87
95
beforeEach ( async ( ) => {
88
96
await remove ( globalCacheDir ) ;
89
- await remove ( path . join ( process . cwd ( ) , "cache" ) ) ;
90
- await remove ( path . join ( process . cwd ( ) , "artifacts" ) ) ;
91
- await getCacheDir ( ) ; // Recreate the cache dir
97
+ await remove ( cacheDir ) ;
98
+ await remove ( artifactsDir ) ;
99
+ await getCacheDir ( ) ; // Calling this recreates the cache dir
92
100
await writeUtf8File ( path . join ( globalCacheDir , "a" ) , "" ) ;
93
- await writeUtf8File ( path . join ( process . cwd ( ) , "cache" , "a" ) , "" ) ;
94
- await writeUtf8File ( path . join ( process . cwd ( ) , "artifacts" , "a" ) , "" ) ;
101
+ await writeUtf8File ( path . join ( cacheDir , "a" ) , "" ) ;
102
+ await writeUtf8File ( path . join ( artifactsDir , "a" ) , "" ) ;
95
103
} ) ;
96
104
97
- await cleanAction ( { global : true } , hre ) ;
98
- assertCleanBehavior ( true , globalCacheDir ) ;
105
+ assertCleanBehavior ( true ) ;
99
106
} ) ;
100
107
101
108
describe ( "when global flag is false" , async ( ) => {
102
109
beforeEach ( async ( ) => {
103
110
await remove ( globalCacheDir ) ;
104
- await getCacheDir ( ) ; // Recreate the cache dir
111
+ await getCacheDir ( ) ; // Calling this recreates the cache dir
105
112
await writeUtf8File ( path . join ( globalCacheDir , "a" ) , "" ) ;
106
113
} ) ;
107
114
108
- await cleanAction ( { global : false } , hre ) ;
109
- assertCleanBehavior ( false , globalCacheDir ) ;
115
+ assertCleanBehavior ( false ) ;
110
116
} ) ;
111
117
} ) ;
112
118
} ) ;
0 commit comments