Skip to content

Commit

Permalink
Merge pull request #27 from Azure/addL0Tests
Browse files Browse the repository at this point in the history
Added L0 tests and yml workflow
  • Loading branch information
eaarora-ms authored Mar 30, 2020
2 parents c1b272b + 71af002 commit 38ad8e8
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 4 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true
}
1 change: 1 addition & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ function main() {
}
});
}
exports.main = main;
main();
72 changes: 72 additions & 0 deletions lib/tests/main.test.js
Original file line number Diff line number Diff line change
@@ -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();
}));
});
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "lib/main.js",
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"repository": {
"type": "git",
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
61 changes: 61 additions & 0 deletions src/tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -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();

});
});

0 comments on commit 38ad8e8

Please sign in to comment.