Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
135 lines (114 loc) · 3.75 KB
/
Copy pathindex.ts
File metadata and controls
135 lines (114 loc) · 3.75 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
123
124
125
126
127
128
129
130
131
132
133
134
135
import fixturez from "fixturez";
import spawn from "spawndamnit";
import path from "path";
import fs from "fs-extra";
/**
* Reason for eslint disable import/no-commonjs
* Technically reassigning imports is not allowed and
* Rollup errors at compile time on this(but the Babel
* transform that's running in jest makes it work there),
* making this a require should be fine.
*/
// eslint-disable-next-line import/no-commonjs
const logger = require("@changesets/logger");
const createLogSilencer = () => {
const originalLoggerError = logger.error;
const originalLoggerInfo = logger.info;
const originalLoggerLog = logger.log;
const originalLoggerWarn = logger.warn;
const originalLoggerSuccess = logger.success;
const originalConsoleError = console.error;
const originalConsoleInfo = console.info;
const originalConsoleLog = console.log;
const originalConsoleWarn = console.warn;
const originalStdoutWrite = process.stdout.write;
const originalStderrWrite = process.stderr.write;
return {
setup() {
logger.error = jest.fn();
logger.info = jest.fn();
logger.log = jest.fn();
logger.warn = jest.fn();
logger.success = jest.fn();
console.error = jest.fn();
console.info = jest.fn();
console.log = jest.fn();
console.warn = jest.fn();
process.stdout.write = jest.fn();
process.stderr.write = jest.fn();
return () => {
logger.error = originalLoggerError;
logger.info = originalLoggerInfo;
logger.log = originalLoggerLog;
logger.warn = originalLoggerWarn;
logger.success = originalLoggerSuccess;
console.error = originalConsoleError;
console.info = originalConsoleInfo;
console.log = originalConsoleLog;
console.warn = originalConsoleWarn;
process.stdout.write = originalStdoutWrite;
process.stderr.write = originalStderrWrite;
};
},
};
};
export const silenceLogsInBlock = () => {
const silencer = createLogSilencer();
let dispose: () => void | undefined;
beforeEach(() => {
dispose = silencer.setup();
});
afterEach(() => {
dispose!();
});
};
export const temporarilySilenceLogs =
(testFn: () => Promise<void> | void) => async () => {
const silencer = createLogSilencer();
const dispose = silencer.setup();
try {
await testFn();
} finally {
dispose();
}
};
let f = fixturez(__dirname);
export interface Fixture extends Record<string, string> {}
export async function testdir(dir: Fixture) {
const temp = f.temp();
await Promise.all(
Object.keys(dir).map(async (filename) => {
const fullPath = path.join(temp, filename);
await fs.outputFile(fullPath, dir[filename]);
})
);
return temp;
}
export const tempdir = f.temp;
export async function gitdir(dir: Fixture) {
const cwd = await testdir(dir);
await spawn("git", ["init"], { cwd });
// so that this works regardless of what the default branch of git init is and for git versions that don't support --initial-branch(like our CI)
{
const { stdout } = await spawn(
"git",
["rev-parse", "--abbrev-ref", "HEAD"],
{ cwd }
);
if (stdout.toString("utf8").trim() !== "main") {
await spawn("git", ["checkout", "-b", "main"], { cwd });
}
}
await spawn("git", ["config", "user.email", "x@y.z"], { cwd });
await spawn("git", ["config", "user.name", "xyz"], { cwd });
await spawn("git", ["config", "commit.gpgSign", "false"], { cwd });
await spawn("git", ["config", "tag.gpgSign", "false"], { cwd });
await spawn("git", ["config", "tag.forceSignAnnotated", "false"], {
cwd,
});
await spawn("git", ["add", "."], { cwd });
await spawn("git", ["commit", "-m", "initial commit", "--allow-empty"], {
cwd,
});
return cwd;
}