Skip to content

Commit d065765

Browse files
authored
Merge pull request #46 from s0/tidying
🎨 Unify folder creation and clearing in tests
2 parents 6cf03fa + 5f03d06 commit d065765

14 files changed

+278
-339
lines changed

action/test/specs/misconfiguration.spec.ts

+36-59
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { rmRF } from '@actions/io';
2-
import * as path from 'path';
3-
41
import * as util from '../util';
2+
import { prepareTestFolders } from '../util/io';
53

64
const KNOWN_HOSTS_WARNING = `
75
##[warning] KNOWN_HOSTS_FILE not set
@@ -22,20 +20,16 @@ that the public key has been added to the target repo
2220

2321
describe('Misconfigurations', () => {
2422
xit('missing-known-hosts', async () => {
25-
const testname = `misconfiguration-missing-known-hosts`;
26-
const dataDir = path.join(util.DATA_DIR, testname);
27-
28-
await rmRF(dataDir);
29-
await util.mkdir(dataDir);
23+
const folders = await prepareTestFolders({ __filename });
3024

3125
// Run Action
3226
await util
3327
.runWithGithubEnv(
34-
testname,
28+
folders.testName,
3529
{
36-
REPO: 'ssh://git@git-ssh/git-server/repos/non-existing.git',
30+
REPO: folders.repoUrl,
3731
BRANCH: 'branch-a',
38-
FOLDER: dataDir,
32+
FOLDER: folders.dataDir,
3933
SSH_PRIVATE_KEY: (
4034
await util.readFile(util.SSH_PRIVATE_KEY)
4135
).toString(),
@@ -63,16 +57,15 @@ describe('Misconfigurations', () => {
6357
});
6458

6559
it('missing-repo', async () => {
66-
const testname = `misconfiguration-missing-repo`;
67-
const dataDir = path.join(util.DATA_DIR, testname);
60+
const folders = await prepareTestFolders({ __filename });
6861

6962
// Run Action
7063
await util
7164
.runWithGithubEnv(
72-
testname,
65+
folders.testName,
7366
{
7467
BRANCH: 'branch-a',
75-
FOLDER: dataDir,
68+
FOLDER: folders.dataDir,
7669
},
7770
's0/test',
7871
{},
@@ -98,14 +91,14 @@ describe('Misconfigurations', () => {
9891
});
9992

10093
it('missing-folder', async () => {
101-
const testname = `misconfiguration-missing-folder`;
94+
const folders = await prepareTestFolders({ __filename });
10295

10396
// Run Action
10497
await util
10598
.runWithGithubEnv(
106-
testname,
99+
folders.testName,
107100
{
108-
REPO: 'ssh://git@git-ssh/git-server/repos/non-existing.git',
101+
REPO: folders.repoUrl,
109102
BRANCH: 'branch-a',
110103
},
111104
's0/test',
@@ -132,16 +125,15 @@ describe('Misconfigurations', () => {
132125
});
133126

134127
it('missing-branch', async () => {
135-
const testname = `misconfiguration-missing-branch`;
136-
const dataDir = path.join(util.DATA_DIR, testname);
128+
const folders = await prepareTestFolders({ __filename });
137129

138130
// Run Action
139131
await util
140132
.runWithGithubEnv(
141-
testname,
133+
folders.testName,
142134
{
143-
REPO: 'ssh://git@git-ssh/git-server/repos/non-existing.git',
144-
FOLDER: dataDir,
135+
REPO: folders.repoUrl,
136+
FOLDER: folders.dataDir,
145137
},
146138
's0/test',
147139
{},
@@ -167,17 +159,16 @@ describe('Misconfigurations', () => {
167159
});
168160

169161
it('missing-event-path', async () => {
170-
const testname = `misconfiguration-missing-event-path`;
171-
const dataDir = path.join(util.DATA_DIR, testname);
162+
const folders = await prepareTestFolders({ __filename });
172163

173164
// Run Action
174165
await util
175166
.runWithGithubEnv(
176-
testname,
167+
folders.testName,
177168
{
178-
REPO: 'ssh://git@git-ssh/git-server/repos/non-existing.git',
169+
REPO: folders.repoUrl,
179170
BRANCH: 'branch-a',
180-
FOLDER: dataDir,
171+
FOLDER: folders.dataDir,
181172
SSH_PRIVATE_KEY: (
182173
await util.readFile(util.SSH_PRIVATE_KEY)
183174
).toString(),
@@ -207,17 +198,16 @@ describe('Misconfigurations', () => {
207198
});
208199

209200
it('missing-ssh-private-key', async () => {
210-
const testname = `misconfiguration-missing-ssh-private-key`;
211-
const dataDir = path.join(util.DATA_DIR, testname);
201+
const folders = await prepareTestFolders({ __filename });
212202

213203
// Run Action
214204
await util
215205
.runWithGithubEnv(
216-
testname,
206+
folders.testName,
217207
{
218-
REPO: 'ssh://git@git-ssh/git-server/repos/non-existing.git',
208+
REPO: folders.repoUrl,
219209
BRANCH: 'branch-a',
220-
FOLDER: dataDir,
210+
FOLDER: folders.dataDir,
221211
},
222212
's0/test',
223213
{},
@@ -245,17 +235,16 @@ describe('Misconfigurations', () => {
245235
});
246236

247237
it('unsupported-http-repo', async () => {
248-
const testname = `misconfiguration-unsupported-http-repo`;
249-
const dataDir = path.join(util.DATA_DIR, testname);
238+
const folders = await prepareTestFolders({ __filename });
250239

251240
// Run Action
252241
await util
253242
.runWithGithubEnv(
254-
testname,
243+
folders.testName,
255244
{
256245
REPO: 'https://github.com/s0/git-publish-subdir-action-tests.git',
257246
BRANCH: 'branch-a',
258-
FOLDER: dataDir,
247+
FOLDER: folders.dataDir,
259248
},
260249
's0/test',
261250
{},
@@ -280,20 +269,16 @@ describe('Misconfigurations', () => {
280269
});
281270
});
282271
it('unauthorized-ssh-key', async () => {
283-
const testname = `unauthorized-ssh-key`;
284-
const dataDir = path.join(util.DATA_DIR, testname);
285-
286-
await rmRF(dataDir);
287-
await util.mkdir(dataDir);
272+
const folders = await prepareTestFolders({ __filename });
288273

289274
// Run Action
290275
await util
291276
.runWithGithubEnv(
292-
testname,
277+
folders.testName,
293278
{
294-
REPO: 'ssh://git@git-ssh/git-server/repos/ssh-no-branch.git',
279+
REPO: folders.repoUrl,
295280
BRANCH: 'branch-a',
296-
FOLDER: dataDir,
281+
FOLDER: folders.dataDir,
297282
SSH_PRIVATE_KEY: (
298283
await util.readFile(util.SSH_PRIVATE_KEY_INVALID)
299284
).toString(),
@@ -320,20 +305,16 @@ describe('Misconfigurations', () => {
320305
});
321306
});
322307
it('self-missing-token', async () => {
323-
const testname = `uself-missing-token`;
324-
const dataDir = path.join(util.DATA_DIR, testname);
325-
326-
await rmRF(dataDir);
327-
await util.mkdir(dataDir);
308+
const folders = await prepareTestFolders({ __filename });
328309

329310
// Run Action
330311
await util
331312
.runWithGithubEnv(
332-
testname,
313+
folders.testName,
333314
{
334315
REPO: 'self',
335316
BRANCH: 'tmp-test-branch',
336-
FOLDER: dataDir,
317+
FOLDER: folders.dataDir,
337318
},
338319
's0/test',
339320
{},
@@ -361,20 +342,16 @@ describe('Misconfigurations', () => {
361342
});
362343

363344
it('self-missing-repo', async () => {
364-
const testname = `uself-missing-repo`;
365-
const dataDir = path.join(util.DATA_DIR, testname);
366-
367-
await rmRF(dataDir);
368-
await util.mkdir(dataDir);
345+
const folders = await prepareTestFolders({ __filename });
369346

370347
// Run Action
371348
await util
372349
.runWithGithubEnv(
373-
testname,
350+
folders.testName,
374351
{
375352
REPO: 'self',
376353
BRANCH: 'tmp-test-branch',
377-
FOLDER: dataDir,
354+
FOLDER: folders.dataDir,
378355
GITHUB_TOKEN: 'foobar',
379356
},
380357
undefined,

action/test/specs/self.spec.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { promises as fs } from 'fs';
22
import * as path from 'path';
3-
import { mkdirP, rmRF } from '@actions/io';
3+
import { mkdirP } from '@actions/io';
44

55
import * as util from '../util';
6-
7-
const DATA_DIR = path.join(util.DATA_DIR, 'self');
6+
import { prepareTestFolders } from '../util/io';
87

98
const RUNNING_IN_GITHUB =
109
!!process.env.GITHUB_SELF_TEST_REPO && !!process.env.GITHUB_SELF_TEST_TOKEN;
@@ -28,19 +27,18 @@ itGithubOnly('Deploy to another branch on self repo', async () => {
2827
);
2928

3029
// Create dummy data
31-
await rmRF(DATA_DIR);
32-
await mkdirP(DATA_DIR);
33-
await mkdirP(path.join(DATA_DIR, 'dummy'));
34-
await fs.writeFile(path.join(DATA_DIR, 'dummy', 'baz'), 'foobar');
35-
await fs.writeFile(path.join(DATA_DIR, 'dummy', '.bat'), 'foobar');
30+
const folders = await prepareTestFolders({ __filename });
31+
await mkdirP(path.join(folders.dataDir, 'dummy'));
32+
await fs.writeFile(path.join(folders.dataDir, 'dummy', 'baz'), 'foobar');
33+
await fs.writeFile(path.join(folders.dataDir, 'dummy', '.bat'), 'foobar');
3634

3735
// Run Action
3836
await util.runWithGithubEnv(
3937
path.basename(__filename),
4038
{
4139
REPO: 'self',
4240
BRANCH: 'tmp-test-branch',
43-
FOLDER: DATA_DIR,
41+
FOLDER: folders.dataDir,
4442
GITHUB_TOKEN: token,
4543
},
4644
repo,

action/test/specs/ssh-custom-messages.spec.ts

+12-19
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
import { promises as fs } from 'fs';
22
import * as path from 'path';
3-
import { mkdirP, rmRF } from '@actions/io';
3+
import { mkdirP } from '@actions/io';
44

55
import * as util from '../util';
6-
7-
const REPO_DIR = path.join(util.REPOS_DIR, 'ssh-custom-messages.git');
8-
const DATA_DIR = path.join(util.DATA_DIR, 'ssh-custom-messages');
6+
import { prepareTestFolders } from '../util/io';
97

108
it('Test custom message templates', async () => {
11-
await rmRF(REPO_DIR);
12-
await rmRF(DATA_DIR);
13-
14-
// Create empty repo
15-
await mkdirP(REPO_DIR);
16-
await util.wrappedExec('git init --bare', { cwd: REPO_DIR });
9+
const folders = await prepareTestFolders({ __filename });
10+
await util.wrappedExec('git init --bare', { cwd: folders.repoDir });
1711

1812
// Create dummy data
19-
await mkdirP(DATA_DIR);
20-
await mkdirP(path.join(DATA_DIR, 'dummy'));
21-
await fs.writeFile(path.join(DATA_DIR, 'dummy', 'baz'), 'foobar');
22-
await fs.writeFile(path.join(DATA_DIR, 'dummy', '.bat'), 'foobar');
13+
await mkdirP(path.join(folders.dataDir, 'dummy'));
14+
await fs.writeFile(path.join(folders.dataDir, 'dummy', 'baz'), 'foobar');
15+
await fs.writeFile(path.join(folders.dataDir, 'dummy', '.bat'), 'foobar');
2316

2417
// Run Action
2518
await util.runWithGithubEnv(
2619
path.basename(__filename),
2720
{
28-
REPO: 'ssh://git@git-ssh/git-server/repos/ssh-custom-messages.git',
21+
REPO: folders.repoUrl,
2922
BRANCH: 'branch-a',
30-
FOLDER: DATA_DIR,
23+
FOLDER: folders.dataDir,
3124
SSH_PRIVATE_KEY: (await fs.readFile(util.SSH_PRIVATE_KEY)).toString(),
3225
KNOWN_HOSTS_FILE: util.KNOWN_HOSTS,
3326
MESSAGE:
@@ -42,9 +35,9 @@ it('Test custom message templates', async () => {
4235
await util.runWithGithubEnv(
4336
path.basename(__filename),
4437
{
45-
REPO: 'ssh://git@git-ssh/git-server/repos/ssh-custom-messages.git',
38+
REPO: folders.repoUrl,
4639
BRANCH: 'branch-a',
47-
FOLDER: DATA_DIR,
40+
FOLDER: folders.dataDir,
4841
SSH_PRIVATE_KEY: (await fs.readFile(util.SSH_PRIVATE_KEY)).toString(),
4942
KNOWN_HOSTS_FILE: util.KNOWN_HOSTS,
5043
MESSAGE: 'This is another commit follow up with no content changes',
@@ -61,7 +54,7 @@ it('Test custom message templates', async () => {
6154
await util.exec(
6255
'git log --pretty="format:msg:%B%ntree:%T%nauthor:%an <%ae>" branch-a',
6356
{
64-
cwd: REPO_DIR,
57+
cwd: folders.repoDir,
6558
}
6659
)
6760
).stdout;

0 commit comments

Comments
 (0)