From c1ac157806a72264963f080c2805cb9ede75132f Mon Sep 17 00:00:00 2001 From: Eavanshi Arora Date: Fri, 27 Mar 2020 18:41:23 +0530 Subject: [PATCH 1/2] Added L0 tests and yml workflow --- .github/workflows/ci.yml | 25 ++++++++++++++ jest.config.js | 10 ++++++ lib/main.js | 1 + lib/tests/main.test.js | 72 ++++++++++++++++++++++++++++++++++++++++ package.json | 8 +++-- src/main.ts | 4 +-- src/tests/main.test.ts | 61 ++++++++++++++++++++++++++++++++++ 7 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 jest.config.js create mode 100644 lib/tests/main.test.js create mode 100644 src/tests/main.test.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 000000000..4a46bc66d --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,25 @@ +on: + pull_request: + branches: + - master + +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@v2 + + - name: 'Validate build' + run: | + npm install + npm run build + + - name: 'Run L0 tests' + run: | + npm run test \ No newline at end of file diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 000000000..45ff45ec8 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,10 @@ +module.exports = { + clearMocks: true, + moduleFileExtensions: ['js', 'ts'], + testEnvironment: 'node', + testMatch: ['**/*.test.ts'], + transform: { + '^.+\\.ts$': 'ts-jest' + }, + verbose: true + } \ No newline at end of file diff --git a/lib/main.js b/lib/main.js index 143e14d9b..1d8954a77 100644 --- a/lib/main.js +++ b/lib/main.js @@ -66,4 +66,5 @@ function main() { } }); } +exports.main = main; main(); diff --git a/lib/tests/main.test.js b/lib/tests/main.test.js new file mode 100644 index 000000000..5e537d1d1 --- /dev/null +++ b/lib/tests/main.test.js @@ -0,0 +1,72 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const core = __importStar(require("@actions/core")); +const main_1 = require("../main"); +const AuthorizerFactory_1 = require("azure-actions-webclient/AuthorizerFactory"); +const ValidatorFactory_1 = require("../ActionInputValidator/ValidatorFactory"); +const DeploymentProviderFactory_1 = require("../DeploymentProvider/DeploymentProviderFactory"); +const actionparameters_1 = require("../actionparameters"); +const PublishProfileWebAppValidator_1 = require("../ActionInputValidator/ActionValidators/PublishProfileWebAppValidator"); +; +const WebAppDeploymentProvider_1 = require("../DeploymentProvider/Providers/WebAppDeploymentProvider"); +jest.mock('@actions/core'); +jest.mock('../actionparameters'); +jest.mock('azure-actions-webclient/AuthorizerFactory'); +jest.mock('../ActionInputValidator/ActionValidators/PublishProfileWebAppValidator'); +jest.mock('../DeploymentProvider/Providers/WebAppDeploymentProvider'); +describe('Test azure-webapps-deploy', () => { + afterEach(() => { + jest.restoreAllMocks(); + }); + it("gets inputs and executes all the functions", () => __awaiter(void 0, void 0, void 0, function* () { + let getAuthorizerSpy = jest.spyOn(AuthorizerFactory_1.AuthorizerFactory, 'getAuthorizer'); + let getActionParamsSpy = jest.spyOn(actionparameters_1.ActionParameters, 'getActionParams'); + let getInputSpy = jest.spyOn(core, 'getInput').mockImplementation((name, options) => { + switch (name) { + case 'publish-profile': return 'MOCK_PUBLISH_PROFILE'; + case 'app-name': return 'MOCK_APP_NAME'; + case 'slot-name': return 'MOCK_SLOT_NAME'; + case 'package': return 'MOCK_PACKAGE'; + case 'images': return 'MOCK_IMAGES'; + case 'configuration-file': return 'MOCK_CONFIGFILE'; + case 'startup-command': return 'MOCK_STARTUP_COMMAND'; + } + return ''; + }); + let getValidatorFactorySpy = jest.spyOn(ValidatorFactory_1.ValidatorFactory, 'getValidator'); + let ValidatorFactoryValidateSpy = jest.spyOn(PublishProfileWebAppValidator_1.PublishProfileWebAppValidator.prototype, 'validate'); + let getDeploymentProviderSpy = jest.spyOn(DeploymentProviderFactory_1.DeploymentProviderFactory, 'getDeploymentProvider'); + let deployWebAppStepSpy = jest.spyOn(WebAppDeploymentProvider_1.WebAppDeploymentProvider.prototype, 'DeployWebAppStep'); + let updateDeploymentStatusSpy = jest.spyOn(WebAppDeploymentProvider_1.WebAppDeploymentProvider.prototype, 'UpdateDeploymentStatus'); + try { + yield main_1.main(); + } + catch (e) { + console.log(e); + } + expect(getAuthorizerSpy).not.toHaveBeenCalled(); // When publish profile is given as input getAuthorizer is not called + expect(getActionParamsSpy).toHaveBeenCalledTimes(2); + expect(getInputSpy).toHaveBeenCalledTimes(1); + expect(getValidatorFactorySpy).toHaveBeenCalledTimes(1); + expect(ValidatorFactoryValidateSpy).toHaveBeenCalledTimes(1); + expect(getDeploymentProviderSpy).toHaveBeenCalledTimes(1); + expect(deployWebAppStepSpy).toHaveBeenCalled(); + expect(updateDeploymentStatusSpy).toHaveBeenCalled(); + })); +}); diff --git a/package.json b/package.json index 074ac8968..e061ccbc8 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "lib/main.js", "scripts": { "build": "tsc", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "jest" }, "repository": { "type": "git", @@ -23,8 +23,12 @@ }, "homepage": "https://github.com/Azure/webapps-deploy#readme", "devDependencies": { + "@types/jest": "^25.1.4", "@types/node": "^13.1.7", - "typescript": "^3.7.4" + "jest": "^25.1.0", + "typescript": "^3.7.4", + "ts-node": "^8.8.1", + "ts-jest": "^25.2.1" }, "dependencies": { "@actions/core": "^1.2.1", diff --git a/src/main.ts b/src/main.ts index b6f6b2994..256243e50 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,10 +11,10 @@ import { ValidatorFactory } from './ActionInputValidator/ValidatorFactory'; var prefix = !!process.env.AZURE_HTTP_USER_AGENT ? `${process.env.AZURE_HTTP_USER_AGENT}` : ""; -async function main() { +export async function main() { let isDeploymentSuccess: boolean = true; - try { + try { // Set user agent variable let usrAgentRepo = crypto.createHash('sha256').update(`${process.env.GITHUB_REPOSITORY}`).digest('hex'); let actionName = 'DeployWebAppToAzure'; diff --git a/src/tests/main.test.ts b/src/tests/main.test.ts new file mode 100644 index 000000000..c89cbc782 --- /dev/null +++ b/src/tests/main.test.ts @@ -0,0 +1,61 @@ +import * as core from "@actions/core"; +import {main} from "../main"; +import { AuthorizerFactory } from "azure-actions-webclient/AuthorizerFactory"; +import { ValidatorFactory } from '../ActionInputValidator/ValidatorFactory'; +import { DeploymentProviderFactory } from '../DeploymentProvider/DeploymentProviderFactory'; +import { ActionParameters} from "../actionparameters"; +import { PublishProfileWebAppValidator } from '../ActionInputValidator/ActionValidators/PublishProfileWebAppValidator';; +import { WebAppDeploymentProvider } from '../DeploymentProvider/Providers/WebAppDeploymentProvider'; + +jest.mock('@actions/core'); +jest.mock('../actionparameters'); +jest.mock('azure-actions-webclient/AuthorizerFactory'); +jest.mock('../ActionInputValidator/ActionValidators/PublishProfileWebAppValidator'); +jest.mock('../DeploymentProvider/Providers/WebAppDeploymentProvider'); + +describe('Test azure-webapps-deploy', () => { + + afterEach(() => { + jest.restoreAllMocks(); + }) + + it("gets inputs and executes all the functions", async () => { + + let getAuthorizerSpy = jest.spyOn(AuthorizerFactory, 'getAuthorizer'); + let getActionParamsSpy = jest.spyOn(ActionParameters, 'getActionParams'); + let getInputSpy = jest.spyOn(core, 'getInput').mockImplementation((name, options) => { + switch(name) { + case 'publish-profile': return 'MOCK_PUBLISH_PROFILE'; + case 'app-name': return 'MOCK_APP_NAME'; + case 'slot-name': return 'MOCK_SLOT_NAME'; + case 'package': return 'MOCK_PACKAGE'; + case 'images': return 'MOCK_IMAGES'; + case 'configuration-file': return 'MOCK_CONFIGFILE'; + case 'startup-command': return 'MOCK_STARTUP_COMMAND'; + } + return ''; + }); + let getValidatorFactorySpy = jest.spyOn(ValidatorFactory, 'getValidator'); + let ValidatorFactoryValidateSpy = jest.spyOn(PublishProfileWebAppValidator.prototype, 'validate'); + let getDeploymentProviderSpy = jest.spyOn(DeploymentProviderFactory, 'getDeploymentProvider'); + let deployWebAppStepSpy = jest.spyOn(WebAppDeploymentProvider.prototype, 'DeployWebAppStep'); + let updateDeploymentStatusSpy = jest.spyOn(WebAppDeploymentProvider.prototype, 'UpdateDeploymentStatus'); + + try { + await main(); + } + catch(e) { + console.log(e); + } + + expect(getAuthorizerSpy).not.toHaveBeenCalled(); // When publish profile is given as input getAuthorizer is not called + expect(getActionParamsSpy).toHaveBeenCalledTimes(2); + expect(getInputSpy).toHaveBeenCalledTimes(1); + expect(getValidatorFactorySpy).toHaveBeenCalledTimes(1); + expect(ValidatorFactoryValidateSpy).toHaveBeenCalledTimes(1); + expect(getDeploymentProviderSpy).toHaveBeenCalledTimes(1); + expect(deployWebAppStepSpy).toHaveBeenCalled(); + expect(updateDeploymentStatusSpy).toHaveBeenCalled(); + + }); +}); \ No newline at end of file From 71af002f1244ec3e6591a79eb37b02e51cd789a1 Mon Sep 17 00:00:00 2001 From: Eavanshi Arora Date: Fri, 27 Mar 2020 18:45:49 +0530 Subject: [PATCH 2/2] Workflow file added --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4a46bc66d..39558bc70 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,5 +21,5 @@ jobs: npm run build - name: 'Run L0 tests' - run: | - npm run test \ No newline at end of file + run: | + npm run test \ No newline at end of file