Skip to content

Commit

Permalink
test: Add integration testing (#131)
Browse files Browse the repository at this point in the history
* docs: Update description
* deps: Add nock
* chore: Add resolveJsonModule
* test: Add integration testing
* chore: Add @typescript-eslint/eslint-plugin
* refactor: Fix lint errors
* chore: Add eslint-plugin-jest
* refactor: Fix lint errors
* test: Add remove working files
* ci: Comment out cache steps
  • Loading branch information
peaceiris committed Jan 18, 2020
1 parent 477d977 commit 386980e
Show file tree
Hide file tree
Showing 16 changed files with 1,249 additions and 96 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:jest/recommended"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
Expand Down
24 changes: 12 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ jobs:
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-
# - 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

Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@

- [gohugoio/hugo: The world’s fastest framework for building websites.](https://github.com/gohugoio/hugo)

We can run **Hugo** on a virtual machine of **GitHub Actions** by this Hugo action. **Hugo extended** version and **Hugo Modules** are supported.
This **Hugo Setup Action** can install **Hugo** to a virtual machine of **GitHub Actions**.
**Hugo extended** version, **Hugo Modules**, Linux (Ubuntu), macOS, and Windows are supported.

From `v2`, this Hugo action migrated to a JavaScript (TypeScript) action. We no longer build or pull a Hugo docker image. Thanks to this change, we can complete this action less than **1 sec**. (A docker base action was taking about 1 min or more execution time to build or pull.)
From `v2`, this Hugo Setup Action has migrated to a JavaScript (TypeScript) action.
We no longer build or pull a Hugo docker image.
Thanks to this change, we can complete this action less than **1 sec**.
(A docker base action was taking about 1 min or more execution time to build and pull a docker image.)

| OS (runs-on) | ubuntu-18.04 | macos-latest | windows-2019 |
|---|:---:|:---:|:---:|
Expand Down
1 change: 1 addition & 0 deletions __tests__/data/brew.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"hugo","full_name":"hugo","oldname":null,"aliases":[],"versioned_formulae":[],"desc":"Configurable static site generator","homepage":"https://gohugo.io/","versions":{"stable":"0.62.2","devel":null,"head":"HEAD","bottle":true},"revision":0,"version_scheme":0,"bottle":{"stable":{"rebuild":0,"cellar":":any_skip_relocation","prefix":"/usr/local","root_url":"https://homebrew.bintray.com/bottles","files":{"catalina":{"url":"https://homebrew.bintray.com/bottles/hugo-0.62.2.catalina.bottle.tar.gz","sha256":"354545c2c125e01a8860f83577fb4218d585fa8d38cd7f51e4228a149347fbcf"},"mojave":{"url":"https://homebrew.bintray.com/bottles/hugo-0.62.2.mojave.bottle.tar.gz","sha256":"9645b64fe6290c4c3b7591ef21139247f0fad6e49da1edd01665b3130a8f1d1a"},"high_sierra":{"url":"https://homebrew.bintray.com/bottles/hugo-0.62.2.high_sierra.bottle.tar.gz","sha256":"0ede4cbcc7536dd6b05107376637840356062273d734b4106be98b3d1732d50c"}}}},"keg_only":false,"bottle_disabled":false,"options":[],"build_dependencies":["go"],"dependencies":[],"recommended_dependencies":[],"optional_dependencies":[],"uses_from_macos":[],"requirements":[],"conflicts_with":[],"caveats":null,"installed":[],"linked_keg":null,"pinned":false,"outdated":false,"analytics":{"install":{"30d":{"hugo":24278,"hugo --HEAD":30},"90d":{"hugo":68639,"hugo --HEAD":80},"365d":{"hugo":223748,"hugo --HEAD":321}},"install_on_request":{"30d":{"hugo":23621,"hugo --HEAD":27},"90d":{"hugo":66676,"hugo --HEAD":74},"365d":{"hugo":215985,"hugo --HEAD":305}},"build_error":{"30d":{"hugo":0}}}}
958 changes: 958 additions & 0 deletions __tests__/data/github.json

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as main from '../src/main';
import * as io from '@actions/io';
import path from 'path';
import nock from 'nock';
// import {FetchError} from 'node-fetch';
import jsonTestBrew from './data/brew.json';
// import jsonTestGithub from './data/github.json';

jest.setTimeout(30000);
const repo = 'hugo';

beforeEach(() => {
jest.resetModules();
});

afterEach(() => {
delete process.env['INPUT_HUGO-VERSION'];
nock.cleanAll();
});

describe('Integration testing run()', () => {
afterEach(async () => {
await io.rmRF(path.join(`${process.env.HOME}`, 'tmp'));
});

test('succeed in installing a custom version', async () => {
const testVersion = '0.61.0';
process.env['INPUT_HUGO-VERSION'] = testVersion;
const result: main.ActionResult = await main.run();
expect(result.exitcode).toBe(0);
expect(result.output).toMatch(`Hugo Static Site Generator v${testVersion}`);
});

test('succeed in installing the latest version', async () => {
const testVersion = 'latest';
process.env['INPUT_HUGO-VERSION'] = testVersion;
nock('https://formulae.brew.sh')
.get(`/api/formula/${repo}.json`)
.reply(200, jsonTestBrew);
const result: main.ActionResult = await main.run();
expect(result.exitcode).toBe(0);
expect(result.output).toMatch('Hugo Static Site Generator v0.62.2');
});
});

describe('showVersion()', () => {
let result: main.ActionResult = {
exitcode: 0,
output: ''
};

test('return version', async () => {
result = await main.showVersion('git', ['--version']);
expect(result.exitcode).toBe(0);
expect(result.output).toMatch(/git version/);
});

test('return not found', async () => {
result = await main.showVersion('gitgit', ['--version']);
expect(result.exitcode).not.toBe(0);
});
});
112 changes: 112 additions & 0 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "Hugo setup action",
"main": "lib/index.js",
"scripts": {
"lint": "eslint ./src/**/*.ts",
"lint:fix": "eslint --fix ./src/**/*.ts",
"lint": "eslint ./{src,__tests__}/**/*.ts",
"lint:fix": "eslint --fix ./{src,__tests__}/**/*.ts",
"test": "jest --coverage --verbose",
"build": "ncc build ./src/index.ts -o lib",
"tsc": "tsc",
Expand Down Expand Up @@ -58,13 +58,16 @@
"@types/jest": "^24.9.0",
"@types/node": "^13.1.7",
"@types/node-fetch": "^2.5.4",
"@typescript-eslint/eslint-plugin": "^2.16.0",
"@typescript-eslint/parser": "^2.16.0",
"@zeit/ncc": "^0.21.0",
"eslint": "^6.8.0",
"eslint-plugin-jest": "^23.6.0",
"husky": "^4.0.10",
"jest": "^24.9.0",
"jest-circus": "^24.9.0",
"lint-staged": "^9.5.0",
"nock": "^11.7.2",
"prettier": "1.19.1",
"standard-version": "^7.0.1",
"ts-jest": "^24.3.0",
Expand Down
7 changes: 7 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum Tool {
Name = 'Hugo',
Org = 'gohugoio',
Repo = 'hugo',
CmdName = 'hugo',
CmdOptVersion = 'version'
}
4 changes: 2 additions & 2 deletions src/get-latest-version.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch from 'node-fetch';

export function getURL(org: string, repo: string, api: string): string {
let url: string = '';
let url = '';

if (api === 'brew') {
url = `https://formulae.brew.sh/api/formula/${repo}.json`;
Expand All @@ -21,7 +21,7 @@ export async function getLatestVersion(
const url = getURL(org, repo, api);
const response = await fetch(url);
const json = await response.json();
let latestVersion: string = '';
let latestVersion = '';
if (api === 'brew') {
latestVersion = json.versions.stable;
} else if (api === 'github') {
Expand Down
2 changes: 1 addition & 1 deletion src/get-os.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function getOS(platform: string) {
export default function getOS(platform: string): string {
if (platform === 'linux') {
return 'Linux';
} else if (platform === 'darwin') {
Expand Down
12 changes: 5 additions & 7 deletions src/get-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default function getURL(
extended: string,
version: string
): string {
const extendedStr = (extended: string) => {
const extendedStr = (extended: string): string => {
if (extended === 'true') {
return 'extended_';
} else {
Expand All @@ -13,19 +13,17 @@ export default function getURL(
}
};

const ext = (os: string) => {
const ext = (os: string): string => {
if (os === 'Windows') {
return 'zip';
} else {
return 'tar.gz';
}
};

const hugoName: string = `hugo_${extendedStr(
extended
)}${version}_${os}-64bit`;
const baseURL: string = 'https://github.com/gohugoio/hugo/releases/download';
const url: string = `${baseURL}/v${version}/${hugoName}.${ext(os)}`;
const hugoName = `hugo_${extendedStr(extended)}${version}_${os}-64bit`;
const baseURL = 'https://github.com/gohugoio/hugo/releases/download';
const url = `${baseURL}/v${version}/${hugoName}.${ext(os)}`;

return url;
}
Loading

0 comments on commit 386980e

Please sign in to comment.