Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: copyAssets and deleteExcludedAssets #528

Merged
merged 7 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __tests__/fixtures/publish_dir_1/assets/lib.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* CSS */
1 change: 1 addition & 0 deletions __tests__/fixtures/publish_dir_1/assets/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// JavaScript
11 changes: 11 additions & 0 deletions __tests__/fixtures/publish_dir_1/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>

</body>
</html>
1 change: 1 addition & 0 deletions __tests__/fixtures/publish_dir_1/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* CSS */
1 change: 1 addition & 0 deletions __tests__/fixtures/publish_dir_1/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// JavaScript
1 change: 1 addition & 0 deletions __tests__/fixtures/publish_dir_root/.github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- issue template -->
7 changes: 7 additions & 0 deletions __tests__/fixtures/publish_dir_root/.github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# dependabot config
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: 'Test'
1 change: 1 addition & 0 deletions __tests__/fixtures/publish_dir_root/assets/lib.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* CSS */
1 change: 1 addition & 0 deletions __tests__/fixtures/publish_dir_root/assets/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// JavaScript
11 changes: 11 additions & 0 deletions __tests__/fixtures/publish_dir_root/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>

</body>
</html>
1 change: 1 addition & 0 deletions __tests__/fixtures/publish_dir_root/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* CSS */
1 change: 1 addition & 0 deletions __tests__/fixtures/publish_dir_root/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// JavaScript
102 changes: 102 additions & 0 deletions __tests__/git-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
copyAssets,
setRepo,
getUserName,
getUserEmail,
Expand All @@ -10,6 +11,17 @@ import {Inputs} from '../src/interfaces';
import {getWorkDirName, createDir} from '../src/utils';
import {CmdResult} from '../src/interfaces';
import * as exec from '@actions/exec';
import {cp, rm} from 'shelljs';
import path from 'path';
import fs from 'fs';

const testRoot = path.resolve(__dirname);

async function createTestDir(name: string): Promise<string> {
const date = new Date();
const unixTime = date.getTime();
return await getWorkDirName(`${unixTime}_${name}`);
}

beforeEach(() => {
jest.resetModules();
Expand All @@ -22,6 +34,96 @@ afterEach(() => {
delete process.env['GITHUB_REPOSITORY'];
});

describe('copyAssets', () => {
let gitTempDir = '';
(async (): Promise<void> => {
peaceiris marked this conversation as resolved.
Show resolved Hide resolved
const date = new Date();
const unixTime = date.getTime();
gitTempDir = await getWorkDirName(`${unixTime}_git`);
})();

beforeAll(async () => {
peaceiris marked this conversation as resolved.
Show resolved Hide resolved
await createDir(gitTempDir);
process.chdir(gitTempDir);
await exec.exec('git', ['init']);
});

test('copy assets from publish_dir to root, delete .github', async () => {
const publishDir = await createTestDir('src');
const destDir = await createTestDir('dst');
cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_1'), publishDir);
cp('-Rf', gitTempDir, destDir);

await copyAssets(publishDir, destDir, '.github');
expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeFalsy();
expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy();
expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy();
rm('-rf', publishDir, destDir);
});

test('copy assets from publish_dir to root, delete .github,main.js', async () => {
peaceiris marked this conversation as resolved.
Show resolved Hide resolved
const publishDir = await createTestDir('src');
const destDir = await createTestDir('dst');
cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_1'), publishDir);
cp('-Rf', gitTempDir, destDir);

await copyAssets(publishDir, destDir, '.github,main.js');
expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeFalsy();
expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy();
expect(fs.existsSync(path.resolve(destDir, 'main.js'))).toBeFalsy();
expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy();
expect(fs.existsSync(path.resolve(destDir, 'assets/lib.js'))).toBeTruthy();
rm('-rf', publishDir, destDir);
});

test('copy assets from publish_dir to root, delete nothing', async () => {
peaceiris marked this conversation as resolved.
Show resolved Hide resolved
const publishDir = await createTestDir('src');
const destDir = await createTestDir('dst');
cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_root'), publishDir);
cp('-Rf', gitTempDir, destDir);

await copyAssets(publishDir, destDir, '');
expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeTruthy();
expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy();
expect(fs.existsSync(path.resolve(destDir, 'main.js'))).toBeTruthy();
expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy();
expect(fs.existsSync(path.resolve(destDir, 'assets/lib.js'))).toBeTruthy();
rm('-rf', publishDir, destDir);
});

test('copy assets from root to root, delete .github', async () => {
peaceiris marked this conversation as resolved.
Show resolved Hide resolved
const publishDir = await createTestDir('src');
const destDir = await createTestDir('dst');
cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_root'), publishDir);
cp('-Rf', gitTempDir, destDir);
cp('-Rf', gitTempDir, publishDir);

await copyAssets(publishDir, destDir, '.github');
expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeFalsy();
expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy();
expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy();
rm('-rf', publishDir, destDir);
});

test('copy assets from root to root, delete nothing', async () => {
peaceiris marked this conversation as resolved.
Show resolved Hide resolved
const publishDir = await createTestDir('src');
const destDir = await createTestDir('dst');
cp('-Rf', path.resolve(testRoot, 'fixtures/publish_dir_root'), publishDir);
cp('-Rf', gitTempDir, destDir);
cp('-Rf', gitTempDir, publishDir);

await copyAssets(publishDir, destDir, '');
expect(fs.existsSync(path.resolve(destDir, '.github'))).toBeTruthy();
expect(fs.existsSync(path.resolve(destDir, 'index.html'))).toBeTruthy();
expect(fs.existsSync(path.resolve(destDir, 'assets/lib.css'))).toBeTruthy();
rm('-rf', publishDir, destDir);
});

test.todo('copy assets from root to subdir, delete .github');
test.todo('copy assets from root to subdir, delete .github,main.js');
test.todo('copy assets from root to subdir, delete nothing');
});

describe('setRepo()', () => {
test('throw error destination_dir should be a relative path', async () => {
process.env['INPUT_GITHUB_TOKEN'] = 'test_github_token';
Expand Down
17 changes: 8 additions & 9 deletions src/git-utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as io from '@actions/io';
import * as glob from '@actions/glob';
import path from 'path';
import fs from 'fs';
import {Inputs, CmdResult} from './interfaces';
import {createDir} from './utils';
import {cp} from 'shelljs';
import {cp, rm} from 'shelljs';

export async function createBranchForce(branch: string): Promise<void> {
await exec.exec('git', ['init']);
Expand All @@ -15,6 +14,7 @@ export async function createBranchForce(branch: string): Promise<void> {
}

export async function deleteExcludedAssets(destDir: string, excludeAssets: string): Promise<void> {
if (excludeAssets === '') return;
core.info(`[INFO] delete excluded assets`);
const excludedAssetNames: Array<string> = excludeAssets.split(',');
const excludedAssetPaths = ((): Array<string> => {
Expand All @@ -25,10 +25,9 @@ export async function deleteExcludedAssets(destDir: string, excludeAssets: strin
return paths;
})();
const globber = await glob.create(excludedAssetPaths.join('\n'));
for await (const asset of globber.globGenerator()) {
core.info(`[INFO] delete ${asset}`);
io.rmRF(asset);
}
const files = await globber.glob();
core.info(`[INFO] delete ${files}`);
rm('-rf', files);
return;
}

Expand All @@ -39,15 +38,15 @@ export async function copyAssets(
): Promise<void> {
core.info(`[INFO] prepare publishing assets`);

if (fs.existsSync(destDir) === false) {
if (!fs.existsSync(destDir)) {
core.info(`[INFO] create ${destDir}`);
await createDir(destDir);
}

const dotGitPath = path.join(publishDir, '.git');
if (fs.existsSync(dotGitPath)) {
core.info(`[INFO] delete .git`);
io.rmRF(dotGitPath);
core.info(`[INFO] delete ${dotGitPath}`);
rm('-rf', dotGitPath);
}

core.info(`[INFO] copy ${publishDir} to ${destDir}`);
Expand Down