-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathconfiguration-variables.ts
155 lines (119 loc) · 4.28 KB
/
configuration-variables.ts
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import assert from "node:assert/strict";
import { before, describe, it } from "node:test";
import { HardhatError } from "@ignored/hardhat-vnext-errors";
import { assertRejectsWithHardhatError } from "@nomicfoundation/hardhat-test-utils";
import { resolveProjectRoot } from "../../../src/index.js";
import { ResolvedConfigurationVariableImplementation } from "../../../src/internal/configuration-variables.js";
import { HookManagerImplementation } from "../../../src/internal/hook-manager.js";
import { UserInterruptionManagerImplementation } from "../../../src/internal/user-interruptions.js";
describe("ResolvedConfigurationVariable", () => {
let hookManager: HookManagerImplementation;
before(async () => {
const projectRoot = await resolveProjectRoot(process.cwd());
hookManager = new HookManagerImplementation(projectRoot, []);
const userInterruptionsManager = new UserInterruptionManagerImplementation(
hookManager,
);
hookManager.setContext({
config: {
tasks: [],
plugins: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
tests: "",
},
},
hooks: hookManager,
globalOptions: {},
interruptions: userInterruptionsManager,
});
});
it("should return the value of a string variable", async () => {
const variable = new ResolvedConfigurationVariableImplementation(
hookManager,
"foo",
);
assert.equal(await variable.get(), "foo");
});
it("should return the value of a configuration variable from an environment variable", async () => {
const variable = new ResolvedConfigurationVariableImplementation(
hookManager,
{ name: "foo", _type: "ConfigurationVariable" },
);
process.env.foo = "bar";
assert.equal(await variable.get(), "bar");
delete process.env.foo;
});
it("should throw if the environment variable is not found", async () => {
const variable = new ResolvedConfigurationVariableImplementation(
hookManager,
{ name: "foo", _type: "ConfigurationVariable" },
);
await assertRejectsWithHardhatError(
variable.get(),
HardhatError.ERRORS.GENERAL.ENV_VAR_NOT_FOUND,
{},
);
});
it("should return the cached value if called multiple times", async () => {
const variable = new ResolvedConfigurationVariableImplementation(
hookManager,
{ name: "foo", _type: "ConfigurationVariable" },
);
process.env.foo = "bar";
assert.equal(await variable.get(), "bar");
process.env.foo = "baz";
assert.equal(await variable.get(), "bar");
delete process.env.foo;
});
it("should return the value of a configuration variable as a URL", async () => {
const variable = new ResolvedConfigurationVariableImplementation(
hookManager,
{ name: "foo", _type: "ConfigurationVariable" },
);
process.env.foo = "http://localhost:8545";
assert.equal(await variable.getUrl(), "http://localhost:8545");
delete process.env.foo;
});
it("should throw if the configuration variable is not a valid URL", async () => {
const variable = new ResolvedConfigurationVariableImplementation(
hookManager,
{ name: "foo", _type: "ConfigurationVariable" },
);
process.env.foo = "not a url";
await assertRejectsWithHardhatError(
variable.getUrl(),
HardhatError.ERRORS.GENERAL.INVALID_URL,
{
url: "not a url",
},
);
delete process.env.foo;
});
it("should return the value of a configuration variable as a BigInt", async () => {
const variable = new ResolvedConfigurationVariableImplementation(
hookManager,
{ name: "foo", _type: "ConfigurationVariable" },
);
process.env.foo = "42";
assert.equal(await variable.getBigInt(), 42n);
delete process.env.foo;
});
it("should throw if the configuration variable is not a valid BigInt", async () => {
const variable = new ResolvedConfigurationVariableImplementation(
hookManager,
{ name: "foo", _type: "ConfigurationVariable" },
);
process.env.foo = "not a bigint";
await assertRejectsWithHardhatError(
variable.getBigInt(),
HardhatError.ERRORS.GENERAL.INVALID_BIGINT,
{
value: "not a bigint",
},
);
delete process.env.foo;
});
});