Skip to content

Commit

Permalink
ACNA-2443: partial lib app config update for install and pack (#723)
Browse files Browse the repository at this point in the history
* ACNA-2443: partial lib app config update for install and pack

* update app config lib
  • Loading branch information
moritzraho committed Sep 7, 2023
1 parent 726ffe7 commit f71eb8d
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 229 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"bugs": "https://github.com/adobe/aio-cli-plugin-app/issues",
"dependencies": {
"@adobe/aio-cli-lib-app-config": "^1.1.0",
"@adobe/aio-cli-lib-app-config-next": "npm:@adobe/aio-cli-lib-app-config@^3.0.0",
"@adobe/aio-cli-lib-console": "^4.1.0",
"@adobe/aio-lib-core-config": "^4.0.0",
"@adobe/aio-lib-core-logging": "^2.0.0",
Expand Down
157 changes: 0 additions & 157 deletions schema/app.config.yaml.schema.json

This file was deleted.

3 changes: 1 addition & 2 deletions schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const { USER_CONFIG_FILE, DEPLOY_CONFIG_FILE, IMPORT_CONFIG_FILE } = require('../src/lib/defaults')
const { DEPLOY_CONFIG_FILE, IMPORT_CONFIG_FILE } = require('../src/lib/defaults')

module.exports = {
[IMPORT_CONFIG_FILE]: require('./config.schema.json'),
[USER_CONFIG_FILE]: require('./app.config.yaml.schema.json'),
[DEPLOY_CONFIG_FILE]: require('./deploy.yaml.schema.json')
}
18 changes: 15 additions & 3 deletions src/commands/app/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const { USER_CONFIG_FILE, DEPLOY_CONFIG_FILE } = require('../../lib/defaults')
const ora = require('ora')
const chalk = require('chalk')

// eslint-disable-next-line node/no-missing-require
const libConfigNext = require('@adobe/aio-cli-lib-app-config-next')

class InstallCommand extends BaseCommand {
async run () {
const { args, flags } = await this.parse(InstallCommand)
Expand All @@ -49,8 +52,8 @@ class InstallCommand extends BaseCommand {
try {
await this.validateZipDirectoryStructure(args.path)
await this.unzipFile(args.path, outputPath)
await this.validateConfig(outputPath, USER_CONFIG_FILE)
await this.validateConfig(outputPath, DEPLOY_CONFIG_FILE)
await this.validateAppConfig(outputPath, USER_CONFIG_FILE)
await this.validateDeployConfig(outputPath, DEPLOY_CONFIG_FILE)
await this.npmInstall(flags.verbose)
await this.runTests()
this.spinner.succeed('Install done.')
Expand Down Expand Up @@ -107,7 +110,16 @@ class InstallCommand extends BaseCommand {
.then(() => this.spinner.succeed(`Extracted app package to ${destFolderPath}`))
}

async validateConfig (outputPath, configFileName, configFilePath = path.join(outputPath, configFileName)) {
async validateAppConfig (outputPath, configFileName, configFilePath = path.join(outputPath, configFileName)) {
this.spinner.start(`Validating ${configFileName}...`)
aioLogger.debug(`validateConfig: ${configFileName} at ${configFilePath}`)
// first coalesce the app config (resolving $include files), then validate it
const config = (await libConfigNext.coalesce(configFilePath)).config
await libConfigNext.validate(config, { throws: true }) // throws on error
this.spinner.succeed(`Validated ${configFileName}`)
}

async validateDeployConfig (outputPath, configFileName, configFilePath = path.join(outputPath, configFileName)) {
this.spinner.start(`Validating ${configFileName}...`)
aioLogger.debug(`validateConfig: ${configFileName} at ${configFilePath}`)

Expand Down
6 changes: 5 additions & 1 deletion src/commands/app/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const { getObjectValue } = require('../../lib/app-helper')
const ora = require('ora')
const chalk = require('chalk')

// eslint-disable-next-line node/no-missing-require
const libConfigNext = require('@adobe/aio-cli-lib-app-config-next')

const DEFAULTS = {
OUTPUT_ZIP_FILE: 'app.zip',
ARTIFACTS_FOLDER: 'app-package',
Expand All @@ -37,7 +40,8 @@ class Pack extends BaseCommand {
aioLogger.debug(`flags: ${JSON.stringify(flags, null, 2)}`)
aioLogger.debug(`args: ${JSON.stringify(args, null, 2)}`)

const appConfig = this.getFullConfig()
// this will also validate the app.config.yaml
const appConfig = await libConfigNext.load()

// resolve to absolute path before any chdir
const outputZipFile = path.resolve(flags.output)
Expand Down
64 changes: 51 additions & 13 deletions test/commands/app/install.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const { USER_CONFIG_FILE, DEPLOY_CONFIG_FILE } = require('../../../src/lib/defau
const path = require('node:path')
const jsYaml = require('js-yaml')

const libConfigNext = require('@adobe/aio-cli-lib-app-config-next')

jest.mock('fs-extra')
jest.mock('unzipper')
jest.mock('../../../src/lib/install-helper')
Expand Down Expand Up @@ -73,6 +75,9 @@ beforeEach(() => {
process.cwd = jest.fn().mockImplementation(() => fakeCwd)
process.chdir.mockClear()
process.cwd.mockClear()

jest.spyOn(libConfigNext, 'coalesce').mockImplementation(async () => ({ config: {} })).mockClear()
jest.spyOn(libConfigNext, 'validate').mockImplementation(async () => {}).mockClear()
})

test('exports', () => {
Expand Down Expand Up @@ -177,7 +182,7 @@ test('unzipFile', async () => {
expect(mockUnzipExtract).toHaveBeenCalledWith(expect.objectContaining({ path: 'my-dest-folder' }))
})

describe('validateConfig', () => {
describe('validateDeployConfig', () => {
test('success', async () => {
installHelper.validateJsonWithSchema.mockReturnValue({
valid: true,
Expand All @@ -187,11 +192,11 @@ describe('validateConfig', () => {
jsYaml.load.mockReturnValue({})

const command = new TheCommand()
await expect(command.validateConfig('my-dest-folder', USER_CONFIG_FILE))
await expect(command.validateDeployConfig('my-dest-folder', DEPLOY_CONFIG_FILE))
.resolves.toEqual(undefined)
expect(installHelper.validateJsonWithSchema).toHaveBeenCalledWith(
expect.any(Object),
USER_CONFIG_FILE
DEPLOY_CONFIG_FILE
)
})

Expand All @@ -202,8 +207,8 @@ describe('validateConfig', () => {
})

const command = new TheCommand()
await expect(command.validateConfig('my-dest-folder', USER_CONFIG_FILE))
.rejects.toThrow(`Missing or invalid keys in ${USER_CONFIG_FILE}:`)
await expect(command.validateDeployConfig('my-dest-folder', DEPLOY_CONFIG_FILE))
.rejects.toThrow(`Missing or invalid keys in ${DEPLOY_CONFIG_FILE}:`)
})
})

Expand Down Expand Up @@ -285,15 +290,18 @@ describe('run', () => {
// since we already unit test the methods above, we mock it here
command.validateZipDirectoryStructure = jest.fn()
command.unzipFile = jest.fn()
command.validateConfig = jest.fn()
command.addCodeDownloadAnnotation = jest.fn()
command.validateDeployConfig = jest.fn()
command.runTests = jest.fn()
command.npmInstall = jest.fn()
command.error = jest.fn()
await command.run()

expect(command.validateZipDirectoryStructure).toHaveBeenCalledTimes(1)
expect(command.unzipFile).toHaveBeenCalledTimes(1)
expect(command.validateConfig).toHaveBeenCalledTimes(2)
expect(libConfigNext.coalesce).toHaveBeenCalledTimes(1)
expect(libConfigNext.validate).toHaveBeenCalledTimes(1)
expect(command.validateDeployConfig).toHaveBeenCalledTimes(1)
expect(command.runTests).toHaveBeenCalledTimes(1)
expect(command.npmInstall).toHaveBeenCalledTimes(1)
expect(command.error).toHaveBeenCalledTimes(0)
Expand All @@ -309,7 +317,8 @@ describe('run', () => {
// we only reject one call, to simulate a subcommand failure
command.validateZipDirectoryStructure = jest.fn()
command.unzipFile = jest.fn()
command.validateConfig = jest.fn()
command.addCodeDownloadAnnotation = jest.fn()
command.validateDeployConfig = jest.fn()
command.npmInstall = jest.fn()
command.error = jest.fn()
command.runTests = jest.fn(() => { throw errorObject })
Expand All @@ -318,7 +327,9 @@ describe('run', () => {

expect(command.validateZipDirectoryStructure).toHaveBeenCalledTimes(1)
expect(command.unzipFile).toHaveBeenCalledTimes(1)
expect(command.validateConfig).toHaveBeenCalledTimes(2)
expect(libConfigNext.coalesce).toHaveBeenCalledTimes(1)
expect(libConfigNext.validate).toHaveBeenCalledTimes(1)
expect(command.validateDeployConfig).toHaveBeenCalledTimes(1)
expect(command.runTests).toHaveBeenCalledTimes(1)
expect(command.npmInstall).toHaveBeenCalledTimes(1)
expect(command.error).toHaveBeenCalledTimes(1)
Expand All @@ -336,7 +347,8 @@ describe('run', () => {
// we only reject one call, to simulate a subcommand failure
command.validateZipDirectoryStructure = jest.fn()
command.unzipFile = jest.fn()
command.validateConfig = jest.fn()
command.addCodeDownloadAnnotation = jest.fn()
command.validateDeployConfig = jest.fn()
command.npmInstall = jest.fn()
command.error = jest.fn()
command.runTests = jest.fn(() => { throw new Error(errorMessage) })
Expand All @@ -345,7 +357,9 @@ describe('run', () => {

expect(command.validateZipDirectoryStructure).toHaveBeenCalledTimes(1)
expect(command.unzipFile).toHaveBeenCalledTimes(1)
expect(command.validateConfig).toHaveBeenCalledTimes(2)
expect(libConfigNext.coalesce).toHaveBeenCalledTimes(1)
expect(libConfigNext.validate).toHaveBeenCalledTimes(1)
expect(command.validateDeployConfig).toHaveBeenCalledTimes(1)
expect(command.runTests).toHaveBeenCalledTimes(1)
expect(command.npmInstall).toHaveBeenCalledTimes(1)
expect(command.error).toHaveBeenCalledTimes(1)
Expand All @@ -360,7 +374,8 @@ describe('run', () => {
// since we already unit test the methods above, we mock it here
command.validateZipDirectoryStructure = jest.fn()
command.unzipFile = jest.fn()
command.validateConfig = jest.fn()
command.addCodeDownloadAnnotation = jest.fn()
command.validateDeployConfig = jest.fn()
command.runTests = jest.fn()
command.npmInstall = jest.fn()
command.error = jest.fn()
Expand All @@ -369,10 +384,33 @@ describe('run', () => {

expect(command.validateZipDirectoryStructure).toHaveBeenCalledTimes(1)
expect(command.unzipFile).toHaveBeenCalledTimes(1)
expect(command.validateConfig).toHaveBeenCalledTimes(2)
expect(libConfigNext.coalesce).toHaveBeenCalledTimes(1)
expect(libConfigNext.validate).toHaveBeenCalledTimes(1)
expect(command.validateDeployConfig).toHaveBeenCalledTimes(1)
expect(command.runTests).toHaveBeenCalledTimes(1)
expect(command.npmInstall).toHaveBeenCalledTimes(1)
expect(command.error).toHaveBeenCalledTimes(0)
expect(fakeCwd).toEqual(path.resolve('my-dest-folder'))
})

test('app config validation error', async () => {
const command = new TheCommand()
command.argv = ['my-app.zip']

// since we already unit test the methods above, we mock it here
command.validateZipDirectoryStructure = jest.fn()
command.unzipFile = jest.fn()
command.addCodeDownloadAnnotation = jest.fn()
command.validateDeployConfig = jest.fn()
command.runTests = jest.fn()
command.npmInstall = jest.fn()
command.error = jest.fn()

const err = new Error('fake validation error')
libConfigNext.validate.mockImplementation(async () => { throw err })
command.spinner.fail = jest.fn()

await command.run()
expect(command.spinner.fail).toHaveBeenCalledWith('fake validation error')
})
})

0 comments on commit f71eb8d

Please sign in to comment.