Skip to content

Commit

Permalink
Merge pull request #9 from technote-space/release/v0.0.8
Browse files Browse the repository at this point in the history
Release/v0.0.8
  • Loading branch information
technote-space committed Sep 22, 2019
2 parents 03e99d3 + a6c7098 commit d49bcdb
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 224 deletions.
6 changes: 3 additions & 3 deletions README.ja.md
Expand Up @@ -47,10 +47,10 @@ logger.info('output info');
// [command]command
// >> stdout1
// >> stdout2
// warning >> stderr1
// warning >> stderr2
// ##[warning] >> stderr1
// ##[warning] >> stderr2
//
// ℹ info output info
// > output info
```

### Command
Expand Down
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -47,10 +47,10 @@ logger.info('output info');
// [command]command
// >> stdout1
// >> stdout2
// warning >> stderr1
// warning >> stderr2
// ##[warning] >> stderr1
// ##[warning] >> stderr2
//
// ℹ info output info
// > output info
```

### Command
Expand Down
14 changes: 8 additions & 6 deletions __tests__/api-helper.test.ts
@@ -1,11 +1,13 @@
/* eslint-disable no-magic-numbers */
import nock from 'nock';
import path from 'path';
import { EOL } from 'os';
import { GitHub } from '@actions/github' ;
import { Response, GitCreateTreeResponse, GitCreateCommitResponse } from '@octokit/rest';
import { spyOnSignale, testLogger } from './util';
import { ApiHelper, Logger } from '../src';
import { disableNetConnect, testEnv, getContext, getApiFixture } from '@technote-space/github-action-test-helper';
import { testLogger } from './util';
import { ApiHelper, Logger } from '../src';
import global from './global';

describe('ApiHelper', () => {
disableNetConnect(nock);
Expand Down Expand Up @@ -247,23 +249,23 @@ describe('ApiHelper', () => {

describe('commit', () => {
it('should not commit 1', async() => {
const {infoMock} = spyOnSignale();
const mockStdout = jest.spyOn(global.mockStdout, 'write');

expect(await helper.commit(path.resolve(__dirname, '..'), 'test commit message', [], octokit, context)).toBeFalsy();

expect(infoMock).toBeCalledWith('There is no diff.');
expect(mockStdout).toBeCalledWith('> There is no diff.' + EOL);
});

it('should not commit 2', async() => {
const {warnMock} = spyOnSignale();
const mockStdout = jest.spyOn(global.mockStdout, 'write');
nock('https://api.github.com')
.persist()
.get('/repos/hello/world/branches/test/protection')
.reply(200, () => getApiFixture(path.resolve(__dirname, 'fixtures'), 'repos.branches.protection'));

expect(await helper.commit(path.resolve(__dirname, 'fixtures'), 'test commit message', ['build1.json', 'build2.json'], octokit, context)).toBeFalsy();

expect(warnMock).toBeCalledWith('Branch [%s] is protected', 'test');
expect(mockStdout).toBeCalledWith('##[warning]Branch [test] is protected' + EOL);
});

it('should commit', async() => {
Expand Down
42 changes: 21 additions & 21 deletions __tests__/command.test.ts
@@ -1,7 +1,8 @@
/* eslint-disable no-magic-numbers */
import { EOL } from 'os';
import global from './global';
import { Logger, Command } from '../src';
import { spyOnSignale, testLogger } from './util';
import { testLogger } from './util';

describe('Command', () => {
testLogger();
Expand All @@ -15,32 +16,31 @@ describe('Command', () => {

it('should run command', async() => {
const execMock = jest.spyOn(global.mockChildProcess, 'exec');
const {logMock} = spyOnSignale();
const mockStdout = jest.spyOn(global.mockStdout, 'write');

expect(await command.execAsync({command: 'test'})).toBe('stdout');

expect(execMock).toBeCalledTimes(1);
expect(execMock.mock.calls[0][0]).toBe('test');
expect(logMock).toBeCalledTimes(2);
expect(logMock.mock.calls[0][0]).toBe('[command]test');
expect(logMock.mock.calls[1][0]).toBe(' >> stdout');
expect(mockStdout).toBeCalledTimes(2);
expect(mockStdout.mock.calls[0][0]).toBe('[command]test' + EOL);
expect(mockStdout.mock.calls[1][0]).toBe(' >> stdout' + EOL);
});

it('should run command with cwd, altCommand', async() => {
global.mockChildProcess.stderr = 'stderr';
const execMock = jest.spyOn(global.mockChildProcess, 'exec');
const {logMock, warnMock} = spyOnSignale();
const mockStdout = jest.spyOn(global.mockStdout, 'write');

expect(await command.execAsync({command: 'test', cwd: 'dir', altCommand: 'alt'})).toBe('stdout');

expect(execMock).toBeCalledTimes(1);
expect(execMock.mock.calls[0][0]).toBe('test');
expect(execMock.mock.calls[0][1]).toEqual({'cwd': 'dir'});
expect(logMock).toBeCalledTimes(2);
expect(logMock.mock.calls[0][0]).toBe('[command]alt');
expect(logMock.mock.calls[1][0]).toBe(' >> stdout');
expect(warnMock).toBeCalledTimes(1);
expect(warnMock.mock.calls[0][0]).toBe(' >> stderr');
expect(mockStdout).toBeCalledTimes(3);
expect(mockStdout.mock.calls[0][0]).toBe('[command]alt' + EOL);
expect(mockStdout.mock.calls[1][0]).toBe(' >> stdout' + EOL);
expect(mockStdout.mock.calls[2][0]).toBe('##[warning] >> stderr' + EOL);
});

it('should catch error 1', async() => {
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('Command', () => {

it('should suppress stdout', async() => {
const execMock = jest.spyOn(global.mockChildProcess, 'exec');
const {logMock} = spyOnSignale();
const mockStdout = jest.spyOn(global.mockStdout, 'write');

await command.execAsync({
command: 'test',
Expand All @@ -94,28 +94,28 @@ describe('Command', () => {

expect(execMock).toBeCalledTimes(1);
expect(execMock.mock.calls[0][0]).toBe('test');
expect(logMock).toBeCalledTimes(1);
expect(logMock.mock.calls[0][0]).toBe('[command]test');
expect(mockStdout).toBeCalledTimes(1);
expect(mockStdout.mock.calls[0][0]).toBe('[command]test' + EOL);
});

it('should not output stdout', async() => {
global.mockChildProcess.stdout = '';
const execMock = jest.spyOn(global.mockChildProcess, 'exec');
const {logMock} = spyOnSignale();
const mockStdout = jest.spyOn(global.mockStdout, 'write');

await command.execAsync({
command: 'test',
});

expect(execMock).toBeCalledTimes(1);
expect(execMock.mock.calls[0][0]).toBe('test');
expect(logMock).toBeCalledTimes(1);
expect(logMock.mock.calls[0][0]).toBe('[command]test');
expect(mockStdout).toBeCalledTimes(1);
expect(mockStdout.mock.calls[0][0]).toBe('[command]test' + EOL);
});

it('should run suppress error command', async() => {
const execMock = jest.spyOn(global.mockChildProcess, 'exec');
const {logMock} = spyOnSignale();
const mockStdout = jest.spyOn(global.mockStdout, 'write');

await command.execAsync({
command: 'test',
Expand All @@ -124,8 +124,8 @@ describe('Command', () => {

expect(execMock).toBeCalledTimes(1);
expect(execMock.mock.calls[0][0]).toBe('test || :');
expect(logMock).toBeCalledTimes(2);
expect(logMock.mock.calls[0][0]).toBe('[command]test');
expect(logMock.mock.calls[1][0]).toBe(' >> stdout');
expect(mockStdout).toBeCalledTimes(2);
expect(mockStdout.mock.calls[0][0]).toBe('[command]test' + EOL);
expect(mockStdout.mock.calls[1][0]).toBe(' >> stdout' + EOL);
});
});
6 changes: 2 additions & 4 deletions __tests__/global.ts
@@ -1,10 +1,8 @@
import { ExecException } from 'child_process';

interface Global extends NodeJS.Global {
mockSignale: {
info: jest.Mock;
log: jest.Mock;
warn: jest.Mock;
mockStdout: {
write: jest.Mock;
};
mockChildProcess: {
exec: jest.Mock;
Expand Down

0 comments on commit d49bcdb

Please sign in to comment.