Skip to content

Commit

Permalink
refactor: Split installer into some functions (#33)
Browse files Browse the repository at this point in the history
* refactor: Add createTempDir()
* refactor: Print tempDir to debug log
* refactor: Add getBaseLocation()
* refactor: Enhance OS type checking
* refactor: Prune core.debug
* refactor: Prune extra async
* refactor: Reduce call count of getBaseLocation()
* refactor: Prune extra initialization
* refactor: Use node-fetch and test with nock
* chore: Add Makefile
* docs: Add make command (Maintainer Notes)
* ci: Enhance YAML list
* style: 💄 reindent
* deps: update
* test: Add nock.cleanAll() to afterEach()
  • Loading branch information
peaceiris authored Jan 5, 2020
1 parent 30be24c commit dd67832
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 173 deletions.
87 changes: 46 additions & 41 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ name: 'Test'

on:
pull_request:
types: [opened, synchronize]
types:
- opened
- synchronize
paths-ignore:
- '**.md'
push:
Expand Down Expand Up @@ -54,45 +56,48 @@ jobs:
needs: code-health
strategy:
matrix:
os: ['ubuntu-18.04', 'macos-latest', 'windows-latest']
os:
- 'ubuntu-18.04'
- 'macos-latest'
- 'windows-latest'
steps:

- uses: actions/checkout@v2

- name: Read .nvmrc
run: echo "::set-output name=NVMRC::$(cat .nvmrc)"
id: nvm

- name: Setup Node
uses: actions/setup-node@v1
with:
node-version: '${{ steps.nvm.outputs.NVMRC }}'

- name: Get npm cache directory
id: npm-cache
run: |
echo "::set-output name=dir::$(npm config get cache)"
- name: Cache npm
uses: actions/cache@v1
with:
path: ${{ steps.npm-cache.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- run: npm ci

- run: npm test

- name: Upload test coverage as artifact
uses: actions/upload-artifact@v1
with:
name: coverage
path: coverage

- name: Upload test coverage to Coveralls
if: startsWith(matrix.os, 'ubuntu')
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@v2

- name: Read .nvmrc
run: echo "::set-output name=NVMRC::$(cat .nvmrc)"
id: nvm

- name: Setup Node
uses: actions/setup-node@v1
with:
node-version: '${{ steps.nvm.outputs.NVMRC }}'

- name: Get npm cache directory
id: npm-cache
run: |
echo "::set-output name=dir::$(npm config get cache)"
- name: Cache npm
uses: actions/cache@v1
with:
path: ${{ steps.npm-cache.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- run: npm ci

- run: npm test

- name: Upload test coverage as artifact
uses: actions/upload-artifact@v1
with:
name: coverage
path: coverage

- name: Upload test coverage to Coveralls
if: startsWith(matrix.os, 'ubuntu')
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmd := "bash"
msg := ""
IMAGE_NAME := actions-mdbook-dev:latest
DOCKER_BUILD := docker build . -t $(IMAGE_NAME) --file ./__tests__/Dockerfile
DOCKER_RUN := docker run --rm -i -t -v ${PWD}:/repo -v ~/.gitconfig:/etc/gitconfig $(IMAGE_NAME)


.PHONY: build
build:
$(DOCKER_BUILD)

.PHONY: run
run:
$(DOCKER_RUN) $(cmd)

.PHONY: test
test:
$(DOCKER_RUN) npm test

.PHONY: commit
commit:
$(DOCKER_RUN) git commit -m "$(msg)"
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,16 @@ Run `npm` and `git commit` commands on a container.

```sh
# Docker
npm run docker:build
npm run docker:run
make build
make run
make run cmd="env"
make test
make commit msg="chore: Add Makefile"
# Release
./release.sh
```

**On Container**

```sh
npm test
git add ./__tests__
git commit -m "test: Add something"
```



<div align="right">
Expand Down
68 changes: 50 additions & 18 deletions __tests__/get-latest-version.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,63 @@
import * as target from '../src/get-latest-version';
import {getURL, getLatestVersion} from '../src/get-latest-version';
const nock = require('nock');
import {FetchError} from 'node-fetch';
import jsonTestBrew from './data/brew.json';
import jsonTestGithub from './data/github.json';

describe('Test', () => {
test('getURL()', () => {
const org: string = 'rust-lang';
const repo: string = 'mdbook';
const urlBrewExpected: string = `https://formulae.brew.sh/api/formula/${repo}.json`;
const urlGithubExpected: string = `https://api.github.com/repos/${org}/${repo}/releases/latest`;
const urlBrew: string = target.getURL(org, repo, 'brew');
const urlGithub: string = target.getURL(org, repo, 'github');
beforeEach(() => {
jest.resetModules();
});

afterEach(() => {
nock.cleanAll();
});

const org: string = 'rust-lang';
const repo: string = 'mdbook';
const urlBrewExpected: string = `https://formulae.brew.sh/api/formula/${repo}.json`;
const urlGithubExpected: string = `https://api.github.com/repos/${org}/${repo}/releases/latest`;

describe('getURL()', () => {
test('should return expected URL', () => {
const urlBrew: string = getURL(org, repo, 'brew');
const urlGithub: string = getURL(org, repo, 'github');

expect(urlBrew).toMatch(urlBrewExpected);
expect(urlGithub).toMatch(urlGithubExpected);
});
});

let versionLatest: string = '0.3.5';
describe('getLatestVersion()', () => {
let versionLatestExpected: string = '0.3.5';

test('getLatestBrew()', () => {
const jsonBrew: target.JsonBrew = jsonTestBrew;
const versionBrew: string = target.getLatestBrew(jsonBrew);
expect(versionBrew).toMatch(versionLatest);
test('should return latest version via brew', async () => {
nock('https://formulae.brew.sh')
.get(`/api/formula/${repo}.json`)
.reply(200, jsonTestBrew);

const versionLatest: string = await getLatestVersion(org, repo, 'brew');
expect(versionLatest).toMatch(versionLatestExpected);
});

test('getLatestGithub()', () => {
const jsonGithub: target.JsonGithub = jsonTestGithub;
const versionGithub: string = target.getLatestGithub(jsonGithub);
expect(versionGithub).toMatch(versionLatest);
test('should return latest version via GitHub', async () => {
nock('https://api.github.com')
.get(`/repos/${org}/${repo}/releases/latest`)
.reply(200, jsonTestGithub);

const versionLatest: string = await getLatestVersion(org, repo, 'github');
expect(versionLatest).toMatch(versionLatestExpected);
});

test('should return exception 404', async () => {
nock('https://formulae.brew.sh')
.get(`/api/formula/${repo}.json`)
.reply(404);

try {
const versionLatest: string = await getLatestVersion(org, repo, 'brew');
console.debug(versionLatest);
} catch (e) {
expect(e).toThrow(FetchError);
}
});
});
2 changes: 1 addition & 1 deletion __tests__/get-os.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getOS from '../src/get-os';
import {getOS} from '../src/get-os';

describe('getOS', () => {
test('test', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/get-url.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getURL from '../src/get-url';
import {getURL} from '../src/get-url';

describe('getURL()', () => {
test('test', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as main from '../src/main';

jest.setTimeout(20000);
jest.setTimeout(30000);

beforeEach(() => {
jest.resetModules();
Expand Down
74 changes: 50 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dd67832

Please sign in to comment.