-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathspec-helper.mts
More file actions
122 lines (108 loc) Β· 3.7 KB
/
spec-helper.mts
File metadata and controls
122 lines (108 loc) Β· 3.7 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
/* node:coverage disable */
import assert from "node:assert/strict"
import { registerHooks } from 'node:module';
import path from "node:path"
import { test, before, after, beforeEach, afterEach, describe, it, type TestContext } from "node:test"
import { MockAgent, setGlobalDispatcher, type Interceptable } from "undici"
// Hook imports to allow cache isolation
registerHooks({
resolve(specifier, context, nextResolve) {
const instanceMatch = specifier.match(/^instance(\d+):\/\//)
const instanceID = instanceMatch ? instanceMatch[1] : null
const strippedSpecifier = instanceMatch ? specifier.substring(instanceMatch[0].length) : specifier
const resolved = nextResolve(strippedSpecifier, context)
if (!resolved.url.startsWith("file://")) {
return resolved
}
const parentKey = context.parentURL ? new URL(context.parentURL).searchParams.get("instance") : null;
if (!instanceID && !parentKey) {
return resolved
}
const resolvedURL = new URL(resolved.url)
resolvedURL.searchParams.set("instance", instanceID || parentKey!)
resolved.url = resolvedURL.toString()
return resolved
}
})
type Test = typeof test
type Before = typeof before
type After = typeof after
type BeforeEach = typeof beforeEach
type AfterEach = typeof afterEach
type Describe = typeof describe
type It = typeof it
type Assert = typeof assert
declare global {
var test: Test
var before: Before
var after: After
var beforeEach: BeforeEach
var afterEach: AfterEach
var describe: Describe
var it: It
var assert: Assert
var mockAgent: MockAgent
var testID: number
function mockInput(input: string, value: string): void
function githubMockPool<TInterceptable extends Interceptable>(): TInterceptable
function loadMain(): Promise<void>
var GITHUB_REPOSITORY: string
}
globalThis.test = test
globalThis.before = before
globalThis.after = after
globalThis.beforeEach = beforeEach
globalThis.afterEach = afterEach
globalThis.describe = describe
globalThis.it = it
globalThis.assert = assert
globalThis.mockInput = function(input, value) {
process.env[`INPUT_${input.replaceAll(" ", "_").toUpperCase()}`] = value
}
globalThis.githubMockPool = function() {
return mockAgent.get("https://api.github.com")
}
globalThis.loadMain = async function() {
const testFile = process.argv[1]
if (!/\.test\.m(?:t|j)s$/.test(testFile)) {
throw new Error("Could not detect test file.")
}
const mainFile = testFile.substring(0, testFile.length - 9) + path.extname(testFile)
await import(`instance${globalThis.testID}://${mainFile}`)
}
// Don't inherit CI environment variables
for (const key of Object.keys(process.env)) {
if (key.startsWith("GITHUB_") || key.startsWith("INPUT_")) {
delete process.env[key]
}
}
// Consistent fake variables
process.env.GITHUB_ACTIONS = "true"
globalThis.GITHUB_REPOSITORY = "fake-owner/fake-repo"
process.env.GITHUB_REPOSITORY = GITHUB_REPOSITORY
process.env.GITHUB_REPOSITORY_OWNER = GITHUB_REPOSITORY.split("/", 1)[0]
const originalEnv = process.env
beforeEach(async (t) => {
process.env = structuredClone(originalEnv)
const agent = new MockAgent()
agent.disableNetConnect()
setGlobalDispatcher(agent)
globalThis.mockAgent = agent
globalThis.testID = (globalThis.testID || 0) + 1;
// Make core.setFailed raise an error rather than print to stdout
(t as TestContext).mock.module(`instance${globalThis.testID}://@actions/core`, {
namedExports: {
...await import(`instance${globalThis.testID}://@actions/core`),
setFailed(error: string | Error) {
if (typeof error === "string") {
throw new Error(error)
} else {
throw error
}
}
}
})
})
afterEach(() => {
mockAgent.assertNoPendingInterceptors()
})