Skip to content

Commit 6cf03fa

Browse files
authored
Merge pull request #45 from s0/tidying
🎨 Improve io and test folder usage
2 parents 69b9f63 + dae84e8 commit 6cf03fa

16 files changed

+735
-148
lines changed

Diff for: action/dist/index.js

+552-21
Large diffs are not rendered by default.

Diff for: action/src/index.ts

+17-22
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
import * as child_process from 'child_process';
22
import { stream as fgStream } from 'fast-glob';
3-
import * as fs from 'fs';
3+
import fsModule, { promises as fs } from 'fs';
44
import gitUrlParse from 'git-url-parse';
55
import { homedir, tmpdir } from 'os';
66
import * as path from 'path';
7-
import { promisify } from 'util';
87
import git from 'isomorphic-git';
9-
10-
const readFile = promisify(fs.readFile);
11-
const copyFile = promisify(fs.copyFile);
12-
const mkdir = promisify(fs.mkdir);
13-
const mkdtemp = promisify(fs.mkdtemp);
14-
const stat = promisify(fs.stat);
15-
const unlink = promisify(fs.unlink);
8+
import { mkdirP } from '@actions/io';
169

1710
export type Console = {
1811
readonly log: (...msg: unknown[]) => void;
@@ -323,7 +316,7 @@ export const main = async ({
323316

324317
// Calculate paths that use temp diractory
325318

326-
const TMP_PATH = await mkdtemp(
319+
const TMP_PATH = await fs.mkdtemp(
327320
path.join(tmpdir(), 'git-publish-subdir-action-')
328321
);
329322
const REPO_TEMP = path.join(TMP_PATH, 'repo');
@@ -332,7 +325,7 @@ export const main = async ({
332325
if (!env.GITHUB_EVENT_PATH) throw new Error('Expected GITHUB_EVENT_PATH');
333326

334327
const event: Event = JSON.parse(
335-
(await readFile(env.GITHUB_EVENT_PATH)).toString()
328+
(await fs.readFile(env.GITHUB_EVENT_PATH)).toString()
336329
);
337330

338331
const name =
@@ -360,7 +353,8 @@ export const main = async ({
360353
// Get the root git directory
361354
let dir = process.cwd();
362355
while (true) {
363-
const isGitRepo = await stat(path.join(dir, '.git'))
356+
const isGitRepo = await fs
357+
.stat(path.join(dir, '.git'))
364358
.then((s) => s.isDirectory())
365359
.catch(() => false);
366360
if (isGitRepo) {
@@ -383,7 +377,7 @@ export const main = async ({
383377

384378
// Get current sha of repo to use in commit message
385379
const gitLog = await git.log({
386-
fs,
380+
fs: fsModule,
387381
depth: 1,
388382
dir,
389383
});
@@ -418,8 +412,8 @@ export const main = async ({
418412
if (!known_hosts) {
419413
log.warn(KNOWN_HOSTS_WARNING);
420414
} else {
421-
await mkdir(SSH_FOLDER, { recursive: true });
422-
await copyFile(known_hosts, KNOWN_HOSTS_TARGET);
415+
await mkdirP(SSH_FOLDER);
416+
await fs.copyFile(known_hosts, KNOWN_HOSTS_TARGET);
423417
}
424418

425419
// Setup ssh-agent with private key
@@ -532,7 +526,7 @@ export const main = async ({
532526
log.log(
533527
`##[info] Using custom glob file to clear target branch ${env.CLEAR_GLOBS_FILE}`
534528
);
535-
const globList = (await readFile(env.CLEAR_GLOBS_FILE))
529+
const globList = (await fs.readFile(env.CLEAR_GLOBS_FILE))
536530
.toString()
537531
.split('\n')
538532
.map((s) => s.trim())
@@ -552,7 +546,7 @@ export const main = async ({
552546
});
553547
// Delete all files from the filestream
554548
for await (const entry of filesToDelete) {
555-
await unlink(entry);
549+
await fs.unlink(entry);
556550
}
557551
const folder = path.resolve(process.cwd(), config.folder);
558552
log.log(`##[info] Copying all files from ${folder}`);
@@ -565,36 +559,37 @@ export const main = async ({
565559
.replace(/\{long\-sha\}/g, gitInfo.sha)
566560
.replace(/\{msg\}/g, gitInfo.commitMessage);
567561
await git.commit({
568-
fs,
562+
fs: fsModule,
569563
dir: REPO_TEMP,
570564
message,
571565
author: { email, name },
572566
});
573567
if (tag) {
574568
log.log(`##[info] Tagging commit with ${tag}`);
575569
await git.tag({
576-
fs,
570+
fs: fsModule,
577571
dir: REPO_TEMP,
578572
ref: tag,
573+
force: true,
579574
});
580575
}
581576
if (config.skipEmptyCommits) {
582577
log.log(`##[info] Checking whether contents have changed before pushing`);
583578
// Before we push, check whether it changed the tree,
584579
// and avoid pushing if not
585580
const head = await git.resolveRef({
586-
fs,
581+
fs: fsModule,
587582
dir: REPO_TEMP,
588583
ref: 'HEAD',
589584
});
590585
const currentCommit = await git.readCommit({
591-
fs,
586+
fs: fsModule,
592587
dir: REPO_TEMP,
593588
oid: head,
594589
});
595590
if (currentCommit.commit.parent.length === 1) {
596591
const previousCommit = await git.readCommit({
597-
fs,
592+
fs: fsModule,
598593
dir: REPO_TEMP,
599594
oid: currentCommit.commit.parent[0],
600595
});

Diff for: action/test/jest-global-setup.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import * as path from 'path';
2-
import * as fs from 'fs';
3-
import {promisify} from 'util';
42

53
import * as util from './util';
64

7-
const copyFile = promisify(fs.copyFile);
8-
95
export = async () => {
106

117
// Generate known-hosts

Diff for: action/test/specs/misconfiguration.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { rmRF } from '@actions/io';
12
import * as path from 'path';
23

34
import * as util from '../util';
@@ -24,6 +25,7 @@ describe('Misconfigurations', () => {
2425
const testname = `misconfiguration-missing-known-hosts`;
2526
const dataDir = path.join(util.DATA_DIR, testname);
2627

28+
await rmRF(dataDir);
2729
await util.mkdir(dataDir);
2830

2931
// Run Action
@@ -281,6 +283,7 @@ describe('Misconfigurations', () => {
281283
const testname = `unauthorized-ssh-key`;
282284
const dataDir = path.join(util.DATA_DIR, testname);
283285

286+
await rmRF(dataDir);
284287
await util.mkdir(dataDir);
285288

286289
// Run Action
@@ -320,6 +323,7 @@ describe('Misconfigurations', () => {
320323
const testname = `uself-missing-token`;
321324
const dataDir = path.join(util.DATA_DIR, testname);
322325

326+
await rmRF(dataDir);
323327
await util.mkdir(dataDir);
324328

325329
// Run Action
@@ -360,6 +364,7 @@ describe('Misconfigurations', () => {
360364
const testname = `uself-missing-repo`;
361365
const dataDir = path.join(util.DATA_DIR, testname);
362366

367+
await rmRF(dataDir);
363368
await util.mkdir(dataDir);
364369

365370
// Run Action

Diff for: action/test/specs/self.spec.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { promises as fs } from 'fs';
12
import * as path from 'path';
3+
import { mkdirP, rmRF } from '@actions/io';
24

35
import * as util from '../util';
46

@@ -26,10 +28,11 @@ itGithubOnly('Deploy to another branch on self repo', async () => {
2628
);
2729

2830
// Create dummy data
29-
await util.mkdir(DATA_DIR);
30-
await util.mkdir(path.join(DATA_DIR, 'dummy'));
31-
await util.writeFile(path.join(DATA_DIR, 'dummy', 'baz'), 'foobar');
32-
await util.writeFile(path.join(DATA_DIR, 'dummy', '.bat'), 'foobar');
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');
3336

3437
// Run Action
3538
await util.runWithGithubEnv(

Diff for: action/test/specs/ssh-custom-messages.spec.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
import { promises as fs } from 'fs';
12
import * as path from 'path';
3+
import { mkdirP, rmRF } from '@actions/io';
24

35
import * as util from '../util';
46

57
const REPO_DIR = path.join(util.REPOS_DIR, 'ssh-custom-messages.git');
68
const DATA_DIR = path.join(util.DATA_DIR, 'ssh-custom-messages');
79

810
it('Test custom message templates', async () => {
11+
await rmRF(REPO_DIR);
12+
await rmRF(DATA_DIR);
13+
914
// Create empty repo
10-
await util.mkdir(REPO_DIR);
15+
await mkdirP(REPO_DIR);
1116
await util.wrappedExec('git init --bare', { cwd: REPO_DIR });
1217

1318
// Create dummy data
14-
await util.mkdir(DATA_DIR);
15-
await util.mkdir(path.join(DATA_DIR, 'dummy'));
16-
await util.writeFile(path.join(DATA_DIR, 'dummy', 'baz'), 'foobar');
17-
await util.writeFile(path.join(DATA_DIR, 'dummy', '.bat'), 'foobar');
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');
1823

1924
// Run Action
2025
await util.runWithGithubEnv(
@@ -23,7 +28,7 @@ it('Test custom message templates', async () => {
2328
REPO: 'ssh://git@git-ssh/git-server/repos/ssh-custom-messages.git',
2429
BRANCH: 'branch-a',
2530
FOLDER: DATA_DIR,
26-
SSH_PRIVATE_KEY: (await util.readFile(util.SSH_PRIVATE_KEY)).toString(),
31+
SSH_PRIVATE_KEY: (await fs.readFile(util.SSH_PRIVATE_KEY)).toString(),
2732
KNOWN_HOSTS_FILE: util.KNOWN_HOSTS,
2833
MESSAGE:
2934
'This is a test message with placeholders:\n* {long-sha}\n* {sha}\n* {branch}',
@@ -40,7 +45,7 @@ it('Test custom message templates', async () => {
4045
REPO: 'ssh://git@git-ssh/git-server/repos/ssh-custom-messages.git',
4146
BRANCH: 'branch-a',
4247
FOLDER: DATA_DIR,
43-
SSH_PRIVATE_KEY: (await util.readFile(util.SSH_PRIVATE_KEY)).toString(),
48+
SSH_PRIVATE_KEY: (await fs.readFile(util.SSH_PRIVATE_KEY)).toString(),
4449
KNOWN_HOSTS_FILE: util.KNOWN_HOSTS,
4550
MESSAGE: 'This is another commit follow up with no content changes',
4651
},

Diff for: action/test/specs/ssh-custom-tags.spec.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import * as fs from 'fs';
1+
import fsModule, { promises as fs } from 'fs';
22
import * as path from 'path';
33
import git from 'isomorphic-git';
4+
import { mkdirP, rmRF } from '@actions/io';
45

56
import * as util from '../util';
67

78
const REPO_DIR = path.join(util.REPOS_DIR, 'ssh-custom-tags.git');
89
const DATA_DIR = path.join(util.DATA_DIR, 'ssh-custom-tags');
910

1011
it('Test custom tags', async () => {
12+
await rmRF(REPO_DIR);
13+
await rmRF(DATA_DIR);
14+
1115
// Create empty repo
12-
await util.mkdir(REPO_DIR);
16+
await mkdirP(REPO_DIR);
1317
await util.wrappedExec('git init --bare', { cwd: REPO_DIR });
1418

1519
// Create dummy data
16-
await util.mkdir(DATA_DIR);
17-
await util.mkdir(path.join(DATA_DIR, 'dummy'));
18-
await util.writeFile(path.join(DATA_DIR, 'dummy', 'baz'), 'foobar');
19-
await util.writeFile(path.join(DATA_DIR, 'dummy', '.bat'), 'foobar');
20+
await mkdirP(DATA_DIR);
21+
await mkdirP(path.join(DATA_DIR, 'dummy'));
22+
await fs.writeFile(path.join(DATA_DIR, 'dummy', 'baz'), 'foobar');
23+
await fs.writeFile(path.join(DATA_DIR, 'dummy', '.bat'), 'foobar');
2024

2125
// Run Action
2226
await util.runWithGithubEnv(
@@ -25,7 +29,7 @@ it('Test custom tags', async () => {
2529
REPO: 'ssh://git@git-ssh/git-server/repos/ssh-custom-tags.git',
2630
BRANCH: 'branch-a',
2731
FOLDER: DATA_DIR,
28-
SSH_PRIVATE_KEY: (await util.readFile(util.SSH_PRIVATE_KEY)).toString(),
32+
SSH_PRIVATE_KEY: (await fs.readFile(util.SSH_PRIVATE_KEY)).toString(),
2933
KNOWN_HOSTS_FILE: util.KNOWN_HOSTS,
3034
},
3135
's0/test',
@@ -40,7 +44,7 @@ it('Test custom tags', async () => {
4044
REPO: 'ssh://git@git-ssh/git-server/repos/ssh-custom-tags.git',
4145
BRANCH: 'branch-a',
4246
FOLDER: DATA_DIR,
43-
SSH_PRIVATE_KEY: (await util.readFile(util.SSH_PRIVATE_KEY)).toString(),
47+
SSH_PRIVATE_KEY: (await fs.readFile(util.SSH_PRIVATE_KEY)).toString(),
4448
KNOWN_HOSTS_FILE: util.KNOWN_HOSTS,
4549
MESSAGE: 'This is another commit follow up with no content changes',
4650
TAG: 'foo-bar-tag-v0.1.2',
@@ -84,12 +88,12 @@ it('Test custom tags', async () => {
8488

8589
// Ensure that commits for branch and tag are identical
8690
const tagSha = await git.resolveRef({
87-
fs,
91+
fs: fsModule,
8892
gitdir: REPO_DIR,
8993
ref: 'refs/tags/foo-bar-tag-v0.1.2',
9094
});
9195
const branchSha = await git.resolveRef({
92-
fs,
96+
fs: fsModule,
9397
gitdir: REPO_DIR,
9498
ref: 'refs/heads/branch-a',
9599
});

Diff for: action/test/specs/ssh-existing-branch-custom-rm-globs.spec.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { promises as fs } from 'fs';
12
import * as path from 'path';
23
import { mkdirP, rmRF } from '@actions/io';
34

@@ -12,20 +13,21 @@ const REPO_CLONE_DIR = path.join(WORK_DIR, 'clone');
1213
const DATA_DIR = path.join(WORK_DIR, 'data');
1314

1415
it('Check that only target deleted files are removed', async () => {
15-
// Create empty repo
1616
await rmRF(REPO_DIR);
17+
await rmRF(WORK_DIR);
18+
19+
// Create empty repo
1720
await mkdirP(REPO_DIR);
1821
await util.wrappedExec('git init --bare', { cwd: REPO_DIR });
1922

2023
// Clone repo, and create an initial commit
21-
await rmRF(WORK_DIR);
2224
await mkdirP(WORK_DIR);
2325
await util.wrappedExec(`git clone "${REPO_DIR}" clone`, { cwd: WORK_DIR });
24-
await util.writeFile(path.join(REPO_CLONE_DIR, 'initial1'), 'foobar1');
25-
await util.writeFile(path.join(REPO_CLONE_DIR, 'initial2'), 'foobar2');
26+
await fs.writeFile(path.join(REPO_CLONE_DIR, 'initial1'), 'foobar1');
27+
await fs.writeFile(path.join(REPO_CLONE_DIR, 'initial2'), 'foobar2');
2628
await mkdirP(path.join(REPO_CLONE_DIR, 'folder'));
27-
await util.writeFile(path.join(REPO_CLONE_DIR, 'folder', 'a'), 'foobar1');
28-
await util.writeFile(path.join(REPO_CLONE_DIR, 'folder', 'b'), 'foobar2');
29+
await fs.writeFile(path.join(REPO_CLONE_DIR, 'folder', 'a'), 'foobar1');
30+
await fs.writeFile(path.join(REPO_CLONE_DIR, 'folder', 'b'), 'foobar2');
2931
await util.wrappedExec(`git add -A .`, { cwd: REPO_CLONE_DIR });
3032
await util.wrappedExec(`git config user.name "Test User"`, {
3133
cwd: REPO_CLONE_DIR,
@@ -37,14 +39,13 @@ it('Check that only target deleted files are removed', async () => {
3739
await util.wrappedExec(`git push origin master`, { cwd: REPO_CLONE_DIR });
3840

3941
// Create dummy data
40-
await rmRF(DATA_DIR);
4142
await mkdirP(path.join(DATA_DIR, 'dummy'));
42-
await util.writeFile(path.join(DATA_DIR, 'dummy', 'baz'), 'foobar');
43-
await util.writeFile(path.join(DATA_DIR, 'dummy', '.bat'), 'foobar');
43+
await fs.writeFile(path.join(DATA_DIR, 'dummy', 'baz'), 'foobar');
44+
await fs.writeFile(path.join(DATA_DIR, 'dummy', '.bat'), 'foobar');
4445

4546
// Setup globs
4647
const globPath = path.join(WORK_DIR, '.globs');
47-
await util.writeFile(
48+
await fs.writeFile(
4849
globPath,
4950
`
5051
folder/*
@@ -60,7 +61,7 @@ it('Check that only target deleted files are removed', async () => {
6061
REPO: `ssh://git@git-ssh/git-server/repos/${UNIQUE_DIRNAME}`,
6162
BRANCH: 'master',
6263
FOLDER: DATA_DIR,
63-
SSH_PRIVATE_KEY: (await util.readFile(util.SSH_PRIVATE_KEY)).toString(),
64+
SSH_PRIVATE_KEY: (await fs.readFile(util.SSH_PRIVATE_KEY)).toString(),
6465
KNOWN_HOSTS_FILE: util.KNOWN_HOSTS,
6566
CLEAR_GLOBS_FILE: globPath,
6667
},

0 commit comments

Comments
 (0)