Skip to content

Commit

Permalink
adding ci using github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Himavanth committed Mar 3, 2020
1 parent 7da4e0f commit 4b56d80
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/commands/app/add/ci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

const BaseCommand = require('../../../BaseCommand')
const yeoman = require('yeoman-environment')
const debug = require('debug')('aio-cli-plugin-app:init')
const { flags } = require('@oclif/command')

class AddCICommand extends BaseCommand {
async run () {
const { args, flags } = this.parse(AddCICommand)

debug(`adding component ${args.component} to the project, using flags: `, flags)

const generator = '@adobe/generator-aio-app/generators/add-ci'
const env = yeoman.createEnv()
env.register(require.resolve(generator), 'gen')
const res = await env.run('gen', {
'skip-prompt': flags.yes
})
return res
}
}

AddCICommand.description = `Add CI files
`

AddCICommand.flags = {
yes: flags.boolean({
description: 'Skip questions, and use all default values',
default: false,
char: 'y'
}),
'skip-install': flags.boolean({
description: 'Skip npm installation after files are created',
default: false
}),
...BaseCommand.flags
}

AddCICommand.args = []

module.exports = AddCICommand
49 changes: 49 additions & 0 deletions src/commands/app/delete/ci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

const BaseCommand = require('../../../BaseCommand')
const yeoman = require('yeoman-environment')
const debug = require('debug')('aio-cli-plugin-app:init')
const { flags } = require('@oclif/command')

class DeleteCICommand extends BaseCommand {
async run () {
const { flags } = this.parse(DeleteCICommand)

debug('deleting CI files from the project, using flags: ', flags)

const generator = '@adobe/generator-aio-app/generators/delete-ci'

const env = yeoman.createEnv()
env.register(require.resolve(generator), 'gen')
const res = await env.run('gen', {
'skip-prompt': flags.yes
})

return res
}
}

DeleteCICommand.description = `Delete existing CI files
`

DeleteCICommand.flags = {
yes: flags.boolean({
description: 'Skip questions, and use all default values',
default: false,
char: 'y'
}),
...BaseCommand.flags
}

DeleteCICommand.args = []

module.exports = DeleteCICommand
101 changes: 101 additions & 0 deletions test/commands/app/add/ci.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const fs = require('fs-extra')

const TheCommand = require('../../../../src/commands/app/add/ci')
const BaseCommand = require('../../../../src/BaseCommand')

jest.mock('fs-extra')

jest.mock('yeoman-environment')
const yeoman = require('yeoman-environment')

const mockRegister = jest.fn()
const mockRun = jest.fn()
yeoman.createEnv.mockReturnValue({
register: mockRegister,
run: mockRun
})

beforeEach(() => {
mockRegister.mockReset()
mockRun.mockReset()
yeoman.createEnv.mockClear()
fs.ensureDirSync.mockClear()
})

describe('Command Prototype', () => {
test('exports', async () => {
expect(typeof TheCommand).toEqual('function')
expect(TheCommand.prototype instanceof BaseCommand).toBeTruthy()
expect(typeof TheCommand.flags).toBe('object')
})
})

describe('bad flags', () => {
test('unknown', async () => {
await expect(TheCommand.run(['--wtf'])).rejects.toThrow('Unexpected argument')
})
})

describe('template module cannot be registered', () => {
test('unknown error', async () => {
mockRegister.mockImplementation(() => { throw new Error('some error') })
await expect(TheCommand.run([])).rejects.toThrow('some error')
})
})

describe('good flags', () => {
test('--yes', async () => {
await TheCommand.run(['--yes'])

expect(yeoman.createEnv).toHaveBeenCalled()
expect(mockRegister).toHaveBeenCalledTimes(1)
const genName = mockRegister.mock.calls[0][1]
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': true
})
})

test('--yes --skip-install', async () => {
await TheCommand.run(['--yes', '--skip-install'])

expect(yeoman.createEnv).toHaveBeenCalled()
expect(mockRegister).toHaveBeenCalledTimes(1)
const genName = mockRegister.mock.calls[0][1]
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': true
})
})

test('--skip-install', async () => {
await TheCommand.run(['--skip-install'])

expect(yeoman.createEnv).toHaveBeenCalled()
expect(mockRegister).toHaveBeenCalledTimes(1)
const genName = mockRegister.mock.calls[0][1]
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': false
})
})

test('no flags', async () => {
await TheCommand.run([])

expect(yeoman.createEnv).toHaveBeenCalled()
expect(mockRegister).toHaveBeenCalledTimes(1)
const genName = mockRegister.mock.calls[0][1]
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': false
})
})
})
79 changes: 79 additions & 0 deletions test/commands/app/delete/ci.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const fs = require('fs-extra')

const TheCommand = require('../../../../src/commands/app/delete/ci')
const BaseCommand = require('../../../../src/BaseCommand')

jest.mock('fs-extra')

jest.mock('yeoman-environment')
const yeoman = require('yeoman-environment')

const mockRegister = jest.fn()
const mockRun = jest.fn()
yeoman.createEnv.mockReturnValue({
register: mockRegister,
run: mockRun
})

beforeEach(() => {
mockRegister.mockReset()
mockRun.mockReset()
yeoman.createEnv.mockClear()
fs.ensureDirSync.mockClear()
})

describe('Command Prototype', () => {
test('exports', async () => {
expect(typeof TheCommand).toEqual('function')
expect(TheCommand.prototype instanceof BaseCommand).toBeTruthy()
expect(typeof TheCommand.flags).toBe('object')
})
})

describe('bad flags', () => {
test('unknown', async () => {
await expect(TheCommand.run(['--wtf'])).rejects.toThrow('Unexpected argument')
})
})

describe('template module cannot be registered', () => {
test('unknown error', async () => {
mockRegister.mockImplementation(() => { throw new Error('some error') })
await expect(TheCommand.run([])).rejects.toThrow('some error')
})
})

describe('good flags', () => {
test('--yes', async () => {
await TheCommand.run(['--yes'])

expect(yeoman.createEnv).toHaveBeenCalled()
expect(mockRegister).toHaveBeenCalledTimes(1)
const genName = mockRegister.mock.calls[0][1]
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': true
})
})

test('no flags', async () => {
await TheCommand.run([])

expect(yeoman.createEnv).toHaveBeenCalled()
expect(mockRegister).toHaveBeenCalledTimes(1)
const genName = mockRegister.mock.calls[0][1]
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': false
})
})
})

0 comments on commit 4b56d80

Please sign in to comment.