1
1
import type { HardhatRuntimeEnvironment } from "@ignored/hardhat-vnext-core/types/hre" ;
2
+ import type repl from "node:repl" ;
2
3
3
4
import assert from "node:assert/strict" ;
4
- import { before , describe , it } from "node:test" ;
5
+ import fs from "node:fs" ;
6
+ import os from "node:os" ;
7
+ import path from "node:path" ;
8
+ import { PassThrough } from "node:stream" ;
9
+ import { afterEach , before , beforeEach , describe , it } from "node:test" ;
5
10
6
11
import { ensureError } from "@ignored/hardhat-vnext-utils/error" ;
7
12
@@ -11,33 +16,56 @@ import { useFixtureProject } from "../../../helpers/project.js";
11
16
12
17
describe ( "console/task-action" , function ( ) {
13
18
let hre : HardhatRuntimeEnvironment ;
19
+ let options : repl . ReplOptions ;
14
20
15
21
before ( async function ( ) {
16
22
hre = await createHardhatRuntimeEnvironment ( { } ) ;
17
23
} ) ;
18
24
25
+ beforeEach ( function ( ) {
26
+ const input = new PassThrough ( ) ;
27
+ const output = new PassThrough ( ) ;
28
+ output . pipe ( process . stdout ) ;
29
+ options = {
30
+ input,
31
+ output,
32
+ } ;
33
+ } ) ;
34
+
19
35
describe ( "javascript" , function ( ) {
20
36
useFixtureProject ( "run-js-script" ) ;
21
37
22
38
it ( "should throw inside the console if script does not exist" , async function ( ) {
23
39
const replServer = await consoleAction (
24
- { commands : [ 'await import("./scripts/non-existent.js");' , ".exit" ] } ,
40
+ {
41
+ commands : [ 'await import("./scripts/non-existent.js");' , ".exit" ] ,
42
+ history : "" ,
43
+ options,
44
+ } ,
25
45
hre ,
26
46
) ;
27
47
ensureError ( replServer . lastError ) ;
28
48
} ) ;
29
49
30
50
it ( "should run a script inside the console successfully" , async function ( ) {
31
51
const replServer = await consoleAction (
32
- { commands : [ 'await import("./scripts/success.js");' , ".exit" ] } ,
52
+ {
53
+ commands : [ ".help" , 'await import("./scripts/success.js");' , ".exit" ] ,
54
+ history : "" ,
55
+ options,
56
+ } ,
33
57
hre ,
34
58
) ;
35
59
assert . equal ( replServer . lastError , undefined ) ;
36
60
} ) ;
37
61
38
62
it ( "should throw inside the console if the script throws" , async function ( ) {
39
63
const replServer = await consoleAction (
40
- { commands : [ 'await import("./scripts/throws.js");' , ".exit" ] } ,
64
+ {
65
+ commands : [ 'await import("./scripts/throws.js");' , ".exit" ] ,
66
+ history : "" ,
67
+ options,
68
+ } ,
41
69
hre ,
42
70
) ;
43
71
ensureError ( replServer . lastError ) ;
@@ -49,26 +77,74 @@ describe("console/task-action", function () {
49
77
50
78
it ( "should throw inside the console if script does not exist" , async function ( ) {
51
79
const replServer = await consoleAction (
52
- { commands : [ 'await import("./scripts/non-existent.ts");' , ".exit" ] } ,
80
+ {
81
+ commands : [ 'await import("./scripts/non-existent.ts");' , ".exit" ] ,
82
+ history : "" ,
83
+ options,
84
+ } ,
53
85
hre ,
54
86
) ;
55
87
ensureError ( replServer . lastError ) ;
56
88
} ) ;
57
89
58
90
it ( "should run a script inside the console successfully" , async function ( ) {
59
91
const replServer = await consoleAction (
60
- { commands : [ 'await import("./scripts/success.ts");' , ".exit" ] } ,
92
+ {
93
+ commands : [ 'await import("./scripts/success.ts");' , ".exit" ] ,
94
+ history : "" ,
95
+ options,
96
+ } ,
61
97
hre ,
62
98
) ;
63
99
assert . equal ( replServer . lastError , undefined ) ;
64
100
} ) ;
65
101
66
102
it ( "should throw inside the console if the script throws" , async function ( ) {
67
103
const replServer = await consoleAction (
68
- { commands : [ 'await import("./scripts/throws.ts");' , ".exit" ] } ,
104
+ {
105
+ commands : [ 'await import("./scripts/throws.ts");' , ".exit" ] ,
106
+ history : "" ,
107
+ options,
108
+ } ,
69
109
hre ,
70
110
) ;
71
111
ensureError ( replServer . lastError ) ;
72
112
} ) ;
73
113
} ) ;
114
+
115
+ describe ( "history" , function ( ) {
116
+ let cacheDir : string ;
117
+ let history : string ;
118
+
119
+ beforeEach ( function ( ) {
120
+ cacheDir = fs . mkdtempSync (
121
+ path . resolve ( os . tmpdir ( ) , "console-action-test-" ) ,
122
+ ) ;
123
+ history = path . resolve ( cacheDir , "console-history.txt" ) ;
124
+ } ) ;
125
+
126
+ afterEach ( function ( ) {
127
+ fs . rmSync ( cacheDir , { recursive : true } ) ;
128
+ } ) ;
129
+
130
+ it ( "should create a history file" , async function ( ) {
131
+ assert . ok (
132
+ ! fs . existsSync ( history ) ,
133
+ "History file exists before running the console" ,
134
+ ) ;
135
+ const replServer = await consoleAction (
136
+ {
137
+ commands : [ ".help" , ".exit" ] ,
138
+ history,
139
+ options,
140
+ } ,
141
+ hre ,
142
+ ) ;
143
+ assert . equal ( replServer . lastError , undefined ) ;
144
+ assert . ok (
145
+ fs . existsSync ( history ) ,
146
+ "History file does not exist after running the console" ,
147
+ ) ;
148
+ } ) ;
149
+ } ) ;
74
150
} ) ;
0 commit comments