From 24a3a128bcbeaa297d6cb091423a1835f8683d80 Mon Sep 17 00:00:00 2001 From: rgsubh <64013610+rgsubh@users.noreply.github.com> Date: Wed, 1 Jul 2020 09:07:14 +0530 Subject: [PATCH] Setup for L0 Testcases (#6) * SetUp for L0 TestCases * Issue Fix * Issue Fix * Issue Fix * Issue Fix * Issue Fix * Issue Fix * Review comments fix: run npm install only with our build, npm install only dev for releases * Running code covrage only on PR of master and releases --- .github/workflows/test.yml | 81 ++++++++++++++++++++++++++++++++++++++ .gitignore | 1 + __tests__/run.test.ts | 7 ++++ jest.config.js | 18 +++++++++ lib/run.js | 2 + package.json | 10 ++++- src/run.ts | 2 +- tsconfig.json | 3 +- 8 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 __tests__/run.test.ts create mode 100644 jest.config.js diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..fbeda9e --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,81 @@ +on: + pull_request: + push: + branches: + - master + - 'releases/*' + +jobs: + build_test_job: + name: 'Build and test job' + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [windows-latest, ubuntu-latest, macos-latest] + steps: + + - name: 'Checking out repo code' + uses: actions/checkout@v1 + + - name: Extract branch name + id: extract_branch + run: | + echo "##[set-output name=branchname;]$(echo ${GITHUB_REF##*/})" + + - name: 'Install dependency for master' + if: github.event.pull_request.base.ref == 'master' || steps.extract_branch.outputs.branchname == 'master' + run: | + npm install + + - name: 'Install dependency for releases' + if: github.event.pull_request.base.ref == 'releases/v1' || steps.extract_branch.outputs.branchname == 'releases/v1' + run: | + npm install --only=dev + + - name: 'Run L0 tests' + run: | + npm run test + + - name : 'Run test coverage' + if: runner.os == 'Windows' && (github.event.pull_request.base.ref == 'releases/v1' || github.event.pull_request.base.ref == 'master') + env: + PR_NUMBER: ${{ github.event.pull_request.number }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + $coverage_result = npm run test-coverage + $start = $false; + $middle = $false; + $end = $false; + $count = 0; + + foreach ($j in $coverage_result) + { + if ($j.tostring().startswith("----------")) + { + if (!$start) + { + $start = $true; + $start_index = $count + } + elseif (!$middle) + { + $middle = $true; + } + elseif (!$end) + { + $end = $true; + $end_index = $count + } + } + $count++ + } + + $tbl_md = $coverage_result[($start_index+1)..($end_index-1)] -join "\n" + $summary = $coverage_result[($end_index + 1)..$count] -join "\n" + $comment = $tbl_md + "\n" + $summary + $url = "https://api.github.com/repos/${env:GITHUB_REPOSITORY}/issues/${env:PR_NUMBER}/comments" + $headers = @{ + "Authorization" = "token ${env:GITHUB_TOKEN}" + } + $body = "{ `"body`": `"${comment}`" }" + Invoke-RestMethod -Method POST -Uri $url -Headers $headers -Body $body \ No newline at end of file diff --git a/.gitignore b/.gitignore index ddea908..d109d82 100644 --- a/.gitignore +++ b/.gitignore @@ -327,3 +327,4 @@ ASALocalRun/ # MFractors (Xamarin productivity tool) working folder .mfractor/ +node_modules diff --git a/__tests__/run.test.ts b/__tests__/run.test.ts new file mode 100644 index 0000000..d5429b6 --- /dev/null +++ b/__tests__/run.test.ts @@ -0,0 +1,7 @@ +import { run } from '../src/run' + +describe('This is a placeholder for intial test cases, to be removed', () => { + test('Dummy test case', async () => { + await expect(run()).rejects.toThrow(); + }) +}) \ No newline at end of file diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..ae50a22 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,18 @@ +module.exports = { + clearMocks: true, + moduleFileExtensions: ['js', 'ts'], + testEnvironment: 'node', + testMatch: ['**/*.test.ts'], + transform: { + '^.+\\.ts$': 'ts-jest' + }, + verbose: true, + coverageThreshold: { + "global": { + "branches": 0, + "functions": 14, + "lines": 27, + "statements": 27 + } + } +} diff --git a/lib/run.js b/lib/run.js index a8badd3..9bc9ee2 100644 --- a/lib/run.js +++ b/lib/run.js @@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); +exports.run = void 0; const os = require("os"); const path = require("path"); const util = require("util"); @@ -80,4 +81,5 @@ function run() { core.setOutput('kubectl-path', cachedPath); }); } +exports.run = run; run().catch(core.setFailed); diff --git a/package.json b/package.json index c71a6c4..98dce99 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "private": true, "main": "lib/run.js", "scripts": { - "build": "tsc --outDir .\\lib\\ --rootDir .\\src\\" + "build": "tsc --outDir .\\lib\\ --rootDir .\\src\\", + "test": "jest", + "test-coverage": "jest --coverage" }, "keywords": [ "actions", @@ -19,6 +21,10 @@ "@actions/tool-cache": "^1.0.0" }, "devDependencies": { - "@types/node": "^12.0.4" + "@types/node": "^12.0.4", + "jest": "^26.0.1", + "@types/jest": "^25.2.2", + "ts-jest": "^25.5.1", + "typescript": "3.9.2" } } diff --git a/src/run.ts b/src/run.ts index f888a7f..07c8545 100644 --- a/src/run.ts +++ b/src/run.ts @@ -64,7 +64,7 @@ async function downloadKubectl(version: string): Promise { return kubectlPath; } -async function run() { +export async function run() { let version = core.getInput('version', { 'required': true }); if (version.toLocaleLowerCase() === 'latest') { version = await getStableKubectlVersion(); diff --git a/tsconfig.json b/tsconfig.json index 7a5d8be..3555b59 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,7 @@ "module": "commonjs" }, "exclude": [ - "node_modules" + "node_modules", + "__tests__" ] } \ No newline at end of file