Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
Fix tests (#4)
Browse files Browse the repository at this point in the history
* Add additional read commands to Makefile

* Add .DS_Store to .gitignore

* Fix tests
  • Loading branch information
fabasoad authored Feb 27, 2021
1 parent f34b606 commit e333786
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 114 deletions.
4 changes: 2 additions & 2 deletions .github.template/workflows/check-updates.yml.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Check updates

on:
schedule:
- cron: '0 0 1,15 * *'
- cron: '0 0 1 * *'

env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
Expand Down Expand Up @@ -38,7 +38,7 @@ jobs:
shell: bash
- name: Create PR
if: ${{ steps.git-diff.outputs.count > 0 }}
uses: peter-evans/create-pull-request@v3.6.0
uses: peter-evans/create-pull-request@v3.8.2
env:
HUSKY: 0
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ jobs:
- uses: actions/checkout@v2.3.4
with:
ref: ${{ github.ref }}
- name: Snyk authenticate
run: ${PM_CLI} run security:auth ${{ secrets.SNYK_API_TOKEN }}
- name: Snyk test
run: ${PM_CLI} run security:test
- uses: snyk/actions/node@master
env:
SNYK_TOKEN: ${{ secrets.SNYK_API_TOKEN }}
codeql_tests:
name: CodeQL
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github.template/workflows/unit-tests.yml.template
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/checkout@v2.3.4
- name: Cache node_modules
id: cache
uses: actions/cache@v2.1.3
uses: actions/cache@v2.1.4
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/${PM_LOCK_FILE}') }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.vscode/*
!.vscode/settings.json
.env
Expand Down
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ default:
read -p "📊 CodeClimate test coverage badge (leave empty if you do not have it): " cc_test_coverage_badge; \
if [[ -z "$$cc_test_coverage_badge" ]] ; then cc_test_coverage_badge=''; else cc_test_coverage_badge="$$cc_test_coverage_badge "; fi; \
export CC_TESTS_COVERAGE_BADGE=$$cc_test_coverage_badge; \
read -p "🔧 Name of a CLI that you want to setup using this GitHub Action, e.g. wren: " cli_name; \
[ -z "$$cli_name" ] && echo '❌ CLI name cannot be empty.' && exit 1; \
export CLI_NAME=$$cli_name; \
read -p "🗄 File extension that this GitHub Action will download to install, e.g. zip (zip): " cli_extension; \
[ -z "$$cli_extension" ] && cli_extension='zip'; \
export CLI_EXTENSION=$$cli_extension; \
read -p "🌐 First part of URL that will be used to download CLI tool, e.g. if url to download CLI tool looks like https://github.com/wren-lang/wren-cli/releases/download/0.3.0/wren_cli-linux-0.3.0.zip then you should enter https://github.com/wren-lang/wren-cli/releases/download: " cli_url; \
[ -z "$$cli_url" ] && echo '❌ CLI URL cannot be empty.' && exit 1; \
export CLI_URL=$$cli_url; \
read -p "🔢 Latest available version of $$cli_name tool: " latest_version; \
[ -z "$$latest_version" ] && echo '❌ Version cannot be empty.' && exit 1; \
export LATEST_VERSION=$$latest_version; \
envsubst < README.md.template > README.md; \
rm -f README.md.template; \
envsubst < action.yml.template > action.yml; \
Expand All @@ -42,14 +54,14 @@ default:
rm -f .github.template/ISSUE_TEMPLATE/feature_request.md.template; \
envsubst < .github.template/workflows/check-updates.yml.template > .github.template/workflows/check-updates.yml; \
rm -f .github.template/workflows/check-updates.yml.template; \
envsubst < .github.template/workflows/security-tests.yml.template > .github.template/workflows/security-tests.yml; \
rm -f .github.template/workflows/security-tests.yml.template; \
envsubst < .github.template/workflows/unit-tests.yml.template > .github.template/workflows/unit-tests.yml; \
rm -f .github.template/workflows/unit-tests.yml.template; \
envsubst < .husky.template/pre-commit.template > .husky.template/pre-commit; \
rm -f .husky.template/pre-commit.template; \
envsubst < .husky.template/pre-push.template > .husky.template/pre-push; \
rm -f .husky.template/pre-push.template
rm -f .husky.template/pre-push.template; \
envsubst < src/consts.ts.template > src/consts.ts; \
rm -f src/consts.ts.template
@rm -rf .github
@mv .github.template .github
@mv .husky.template .husky
Expand Down
5 changes: 5 additions & 0 deletions action.yml.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ description: '${REPO_TITLE} GitHub Action'
branding:
icon: terminal
color: gray-dark
inputs:
version:
description: '${CLI_NAME} version.'
required: false
default: ${LATEST_VERSION}
runs:
using: 'node12'
main: 'dist/index.js'
20 changes: 10 additions & 10 deletions package.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@
"winston": "3.3.3"
},
"devDependencies": {
"@types/chai": "4.2.14",
"@types/chai": "4.2.15",
"@types/jest": "26.0.20",
"@types/node": "14.14.21",
"@typescript-eslint/eslint-plugin": "4.13.0",
"@typescript-eslint/parser": "4.13.0",
"@types/node": "14.14.31",
"@typescript-eslint/eslint-plugin": "4.15.2",
"@typescript-eslint/parser": "4.15.2",
"@vercel/ncc": "0.27.0",
"chai": "4.2.0",
"eslint": "7.18.0",
"chai": "4.3.0",
"eslint": "7.20.0",
"eslint-config-google": "0.14.0",
"git-branch-is": "4.0.0",
"husky": "5.0.6",
"husky": "5.1.1",
"jest": "26.6.3",
"jest-circus": "26.6.3",
"markdownlint-cli": "0.26.0",
"mocha-param": "2.0.1",
"snyk": "1.437.4",
"ts-jest": "26.4.4",
"typescript": "4.1.3"
"snyk": "1.461.0",
"ts-jest": "26.5.2",
"typescript": "4.2.2"
},
"snyk": true
}
6 changes: 3 additions & 3 deletions src/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { addPath } from '@actions/core'
import { cacheDir } from '@actions/tool-cache'
import fs from 'fs'
import { chmodSync } from 'fs'
import path from 'path'
import { Logger } from 'winston'
import CliExeNameProvider from './CliExeNameProvider'
Expand All @@ -13,14 +13,14 @@ export default class Cache implements ICache {

constructor(
version: string,
provider: ICliExeNameProvider = new CliExeNameProvider()) {
provider: ICliExeNameProvider = new CliExeNameProvider(version)) {
this.version = version
this.provider = provider
this.log = LoggerFactory.create('Cache')
}

async cache(execFilePath: string): Promise<void> {
fs.chmodSync(execFilePath, '777')
chmodSync(execFilePath, '777')
this.log.info(
`Access permissions of ${execFilePath} file was changed to 777.`)
const folderPath: string = path.dirname(execFilePath)
Expand Down
4 changes: 2 additions & 2 deletions src/Downloader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { downloadTool } from '@actions/tool-cache'
import fs from 'fs'
import { Logger } from 'winston'
import { CLI_NAME } from './consts'
import { CLI_EXTENSION, CLI_NAME } from './consts'
import LoggerFactory from './LoggerFactory'

export default class Downloader implements IDownloader {
Expand All @@ -11,7 +11,7 @@ export default class Downloader implements IDownloader {
this.log.info(`Downloading ${CLI_NAME} from ${url}`)
const zipPathOld: string = await downloadTool(url)
this.log.info(`Downloaded to ${zipPathOld}`)
const zipPathNew: string = zipPathOld + '.zip'
const zipPathNew: string = zipPathOld + '.' + CLI_EXTENSION
fs.renameSync(zipPathOld, zipPathNew)
this.log.info(`Renamed to ${zipPathNew}`)
return zipPathNew
Expand Down
3 changes: 2 additions & 1 deletion src/UrlProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import CliFileNameBuilder from './CliFileNameBuilder'
import { CLI_EXTENSION, CLI_URL } from './consts'

export default class UrlProvider implements IUrlProvider {
private builder: ICliFileNameBuilder
Expand All @@ -12,6 +13,6 @@ export default class UrlProvider implements IUrlProvider {
}

getUrl(): string {
return '{PROJECT_URL}' + `${this.version}/${this.builder.build()}.zip`
return `${CLI_URL}/${this.version}/${this.builder.build()}.${CLI_EXTENSION}`
}
}
42 changes: 20 additions & 22 deletions src/__tests__/Cache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
import * as core from '@actions/core'
import * as tc from '@actions/tool-cache'
import fs from 'fs'
import { addPath } from '@actions/core'
import { cacheDir } from '@actions/tool-cache'
import { chmodSync } from 'fs'
import path from 'path'
import { restore, SinonStub, stub } from 'sinon'
import Cache from '../Cache'

describe('Cache', () => {
let addPathStub: SinonStub<[inputPath: string], void>
let cacheDirStub: SinonStub<[sourceDir: string,
tool: string, version: string, arch?: string], Promise<string>>
let chmodSyncStub: SinonStub<[path: fs.PathLike, mode: fs.Mode], void>

beforeEach(() => {
addPathStub = stub(core, 'addPath')
cacheDirStub = stub(tc, 'cacheDir')
chmodSyncStub = stub(fs, 'chmodSync')
})
jest.mock('@actions/core', () => ({ addPath: jest.fn() }));
jest.mock('@actions/tool-cache', () => ({ cacheDir: jest.fn() }));
jest.mock('fs', () => ({ chmodSync: jest.fn() }));

describe('Cache', () => {
it('should cache successfully', async () => {
const version: string = 'ey1r6c00'
const exeFileName: string = 'O7DF0gox'
const getExeFileNameMock: jest.Mock<string, []> = jest.fn(() => exeFileName)
const folderPath: string = '1ef84ehe'
const execFilePath: string = path.join(folderPath, 'm8x9p1sw')
const cachedPath: string = '1r4wn1iw'
cacheDirStub.returns(Promise.resolve(cachedPath))
const cachedPath: string = '1r4wn1iw';
(cacheDir as jest.Mock)
.mockImplementation(() => Promise.resolve(cachedPath))
const cache: Cache = new Cache(version, {
getExeFileName: getExeFileNameMock
})
await cache.cache(execFilePath)

expect(getExeFileNameMock.mock.calls.length).toBe(1)
expect(chmodSyncStub.withArgs(execFilePath, '777').callCount).toBe(1)
expect(cacheDirStub.withArgs(folderPath, exeFileName, version).callCount)
.toBe(1)
expect(addPathStub.withArgs(cachedPath).callCount).toBe(1)
expect((chmodSync as jest.Mock).mock.calls.length).toBe(1)
expect(chmodSync).toHaveBeenCalledWith(execFilePath, '777')
expect((addPath as jest.Mock).mock.calls.length).toBe(1)
expect((cacheDir as jest.Mock).mock.calls.length).toBe(1)
expect(cacheDir).toHaveBeenCalledWith(folderPath, exeFileName, version)
})

afterEach(() => restore())
afterEach(() => {
(addPath as jest.Mock).mockClear(),
(cacheDir as jest.Mock).mockClear(),
(chmodSync as jest.Mock).mockClear()
})
})
15 changes: 5 additions & 10 deletions src/__tests__/CliExeNameProvider.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import itParam from 'mocha-param'
import os from 'os'
import { restore, SinonStub, stub } from 'sinon'
import { type } from 'os'
import CliExeNameProvider from '../CliExeNameProvider'
import { CLI_NAME } from '../consts'

jest.mock('os', () => ({ type: jest.fn() }))

interface IFixture {
os: string
execFileName: string
}

describe('CliExeNameProvider', () => {
let osTypeStub: SinonStub<[], string>

const expectedVersion: string = 'ey1r6c00'
const items: IFixture[] = [{
os: 'Windows_NT',
Expand All @@ -24,16 +23,12 @@ describe('CliExeNameProvider', () => {
execFileName: CLI_NAME
}]

beforeEach(() => {
osTypeStub = stub(os, 'type')
})

itParam('should return exe name successfully', items, (item: IFixture) => {
osTypeStub.returns(item.os)
(type as jest.Mock).mockImplementation(() => item.os)
const provider: CliExeNameProvider = new CliExeNameProvider(expectedVersion)
const actual: string = provider.getExeFileName()
expect(actual).toBe(item.execFileName)
})

afterEach(() => restore())
afterEach(() => (type as jest.Mock).mockClear())
})
15 changes: 5 additions & 10 deletions src/__tests__/CliFileNameBuilder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import itParam from 'mocha-param'
import os from 'os'
import { restore, SinonStub, stub } from 'sinon'
import { type } from 'os'
import CliFileNameBuilder from '../CliFileNameBuilder'
import { CLI_NAME } from '../consts'

jest.mock('os', () => ({ type: jest.fn() }))

interface IFixture {
os1: string
os2: string
Expand All @@ -22,18 +23,12 @@ describe('CliFileNameBuilder', () => {
os2: 'linux'
}]

let osTypeStub: SinonStub<[], string>

beforeEach(() => {
osTypeStub = stub(os, 'type')
})

itParam('should build successfully (${value.os1})',
items, (item: IFixture) => {
osTypeStub.returns(item.os1)
(type as jest.Mock).mockImplementation(() => item.os1)
const b: CliFileNameBuilder = new CliFileNameBuilder(expectedVersion)
expect(b.build()).toBe(`${CLI_NAME}-${item.os2}-${expectedVersion}`)
})

afterEach(() => restore())
afterEach(() => (type as jest.Mock).mockClear())
})
33 changes: 16 additions & 17 deletions src/__tests__/Downloader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import * as tc from '@actions/tool-cache'
import fs from 'fs'
import { restore, SinonStub, stub } from 'sinon'
import { downloadTool } from '@actions/tool-cache'
import { renameSync } from 'fs'
import { CLI_EXTENSION } from '../consts'
import Downloader from '../Downloader'

describe('Downloader', () => {
let fsRenameSyncStub:
SinonStub<[oldPath: fs.PathLike, newPath: fs.PathLike], void>
let downloadToolStub: SinonStub

beforeEach(() => {
fsRenameSyncStub = stub(fs, 'renameSync')
downloadToolStub = stub(tc, 'downloadTool')
})
jest.mock('@actions/tool-cache', () => ({ downloadTool: jest.fn() }))
jest.mock('fs', () => ({ renameSync: jest.fn() }))

describe('Downloader', () => {
it('should download successfully', async () => {
const zipPathOld: string = 'yw86z9qw'
const zipPathNew: string = zipPathOld + '.zip'
const url: string = '9r1y2ryp'
downloadToolStub.returns(Promise.resolve(zipPathOld))
const zipPathNew: string = zipPathOld + '.' + CLI_EXTENSION
const url: string = '9r1y2ryp';
(downloadTool as jest.Mock)
.mockImplementation(() => Promise.resolve(zipPathOld))
const d: Downloader = new Downloader()
const actual: string = await d.download(url)
expect(fsRenameSyncStub.withArgs(zipPathOld, zipPathNew).callCount).toBe(1)
expect((renameSync as jest.Mock).mock.calls.length).toBe(1)
expect(renameSync).toHaveBeenCalledWith(zipPathOld, zipPathNew)
expect(actual).toBe(zipPathNew)
})

afterEach(() => restore())
afterEach(() => {
(downloadTool as jest.Mock).mockClear();
(renameSync as jest.Mock).mockClear()
})
})
Loading

0 comments on commit e333786

Please sign in to comment.