Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: create-cli cleanup #792

Merged
merged 25 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7eb7f70
chore: convert create cli to commonjs [gh-0]
umutuzgur Jul 5, 2023
191fd4e
chore: convert create cli to commonjs [gh-0]
umutuzgur Jul 5, 2023
d951739
Merge branch 'main' into umutuzgur/gh-0/create-cli-cleanup
danielpaulus Jul 5, 2023
246b3bf
chore: print err
danielpaulus Jul 5, 2023
99c09cb
chore: move ts build back to prepare [sc-0]
umutuzgur Jul 25, 2023
e0084b5
chore: move ts build back to prepare [sc-0]
umutuzgur Jul 25, 2023
6abb2c8
Merge branch 'main' into umutuzgur/gh-0/create-cli-cleanup
Jul 26, 2023
75cfccd
chore: remove all ESM packages/references [gh-0] (#803)
Aug 1, 2023
597338d
Merge branch 'main' into umutuzgur/gh-0/create-cli-cleanup
Aug 1, 2023
477dbae
test: add create-cli tests to github actions test workflow [gh-0]
Aug 1, 2023
8219665
test: fix username on CI/CD [gh-0]
Aug 1, 2023
855f5a8
test: move unit tests with lint [gh-0]
Aug 1, 2023
f5e4c38
revert: test workflow changes [gh-0]
Aug 1, 2023
54495ec
revert: error message [gh-0]
Aug 1, 2023
016b121
revert: prepare script rename [gh-0]
Aug 1, 2023
140dd6f
revert: prepare script rename [gh-0]
Aug 1, 2023
69845ec
chore: rename create-cli command wrapper [gh-0]
Aug 2, 2023
2f58000
chore: add retry for template download [gh-0]
Aug 2, 2023
02fbdc2
Merge branch 'main' into umutuzgur/gh-0/create-cli-cleanup
Aug 2, 2023
828afb4
test: command cancelled error output [gh-0]
Aug 2, 2023
5d934a0
test: add old version test [gh-0]
Aug 3, 2023
d43cb9b
Merge branch 'main' into umutuzgur/gh-0/create-cli-cleanup
Aug 3, 2023
8ad38cc
chore: add v prefix on templatePath [gh-0]
Aug 3, 2023
a4f5a84
Merge branch 'main' into umutuzgur/gh-0/create-cli-cleanup
Aug 3, 2023
e1b4cbc
chore: remove v prefix [gh-0]
Aug 3, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ local
.tsbuildinfo
**/checkly-github-report.md
**/checkly-summary.md
**/e2e/__tests__/fixtures/empty-project/e2e-test-project-*
2,218 changes: 600 additions & 1,618 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ if (requiredVersion < minimumVersion) {
process.exit(1)
}

import('./dist/index.js').then(({ main }) => main())
const oclif = require('@oclif/core')

oclif.run().then(require('@oclif/core/flush')).catch(require('@oclif/core/handle'))
3 changes: 3 additions & 0 deletions packages/create-cli/bin/run.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off

node "%~dp0\run" %*
275 changes: 275 additions & 0 deletions packages/create-cli/e2e/__tests__/bootstrap.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
import axios from 'axios'
import * as rimraf from 'rimraf'
import * as path from 'path'
import * as fs from 'fs'
import { v4 as uuidv4 } from 'uuid'
import { runChecklyCreateCli } from '../run-create-cli'
import { getUserGreeting } from '../../src/utils/messages'
import { PROJECT_TEMPLATES } from '../../src/utils/prompts'
import { SpawnSyncReturns } from 'child_process'

const E2E_PROJECT_PREFIX = 'e2e-test-project-'

function cleanupProjects () {
rimraf.sync(`${path.join(__dirname, 'fixtures', 'empty-project', E2E_PROJECT_PREFIX)}*`, { glob: true })
rimraf.windowsSync(`${path.join(__dirname, 'fixtures', 'empty-project', E2E_PROJECT_PREFIX)}*`, { glob: true })
}

function expectVersionAndName ({
commandOutput,
version,
latestVersion,
greeting,
}: {
commandOutput: SpawnSyncReturns<string>
version?: string
latestVersion: string
greeting: string
}) {
if (!version) {
expect(commandOutput.stdout).toContain(`Notice: replacing version '0.0.1-dev' with latest '${latestVersion}'.`)
}
expect(commandOutput.stdout).toContain(`v${version ?? latestVersion} Build and Run Synthetics That Scale`)
expect(commandOutput.stdout).toContain(`${greeting} Let's get you started on your monitoring as code journey!`)
}

function expectCompleteCreation ({
commandOutput,
projectFolder,
}: {
commandOutput: SpawnSyncReturns<string>
projectFolder: string
}) {
expect(commandOutput.stdout).toContain(`All done. Time to get testing & monitoring with Checkly

> Enter your project directory using cd ${projectFolder}
> Run npx checkly login to login to your Checkly account
> Run npx checkly test to dry run your checks
> Run npx checkly deploy to deploy your checks to the Checkly cloud

Questions?

- Check the docs at https://checklyhq.com/docs/cli
- Join the Checkly Slack community at https://checklyhq.com/slack`)
}

describe('bootstrap', () => {
let latestVersion = ''
let projectName = ''
let greeting = ''
beforeAll(async () => {
const packageInformation = await axios.get('https://registry.npmjs.org/checkly/latest')
latestVersion = packageInformation.data.version
greeting = await getUserGreeting()
})
beforeEach(() => {
projectName = `${E2E_PROJECT_PREFIX}${uuidv4()}`
})
afterAll(() => cleanupProjects())

it('Should create project with advanced-project template', () => {
const directory = path.join(__dirname, 'fixtures', 'empty-project')
const projectFolder = path.join(directory, projectName)
const commandOutput = runChecklyCreateCli({
directory,
promptsInjection: [projectName, 'advanced-project', true, true],
})

const { status, stdout, stderr } = commandOutput

expectVersionAndName({ commandOutput, latestVersion, greeting })

expect(stdout).toContain('Downloading example template...')
expect(stdout).toContain('Example template copied!')
expect(stdout).toContain('installing packages')
expect(stdout).toContain('Packages installed successfully')

expect(stderr).toBe('')
expect(status).toBe(0)

expect(fs.existsSync(path.join(projectFolder, 'package.json'))).toBe(true)
expect(fs.existsSync(path.join(projectFolder, 'checkly.config.ts'))).toBe(true)
expect(fs.existsSync(path.join(projectFolder, 'node_modules'))).toBe(true)
expect(fs.existsSync(path.join(projectFolder, '.git'))).toBe(true)
}, 30000)

it('Should create an boilerplate-project without installing dependencies', () => {
const directory = path.join(__dirname, 'fixtures', 'empty-project')
const projectFolder = path.join(directory, projectName)
const commandOutput = runChecklyCreateCli({
directory,
promptsInjection: [projectName, 'boilerplate-project', false, false],
})

expectVersionAndName({ commandOutput, latestVersion, greeting })

const { status, stdout, stderr } = commandOutput

expect(stdout).toContain('Downloading example template...')
expect(stdout).toContain('Example template copied!')
expect(stdout).not.toContain('installing packages')
expect(stdout).not.toContain('Packages installed successfully')

// no git initialization message
expect(stdout).toContain('No worries. Just remember to install the dependencies after this setup')

expectCompleteCreation({ commandOutput, projectFolder })

expect(stderr).toBe('')
expect(status).toBe(0)

expect(fs.existsSync(path.join(projectFolder, 'package.json'))).toBe(true)
expect(fs.existsSync(path.join(projectFolder, 'checkly.config.ts'))).toBe(true)

// node_modules nor .git shouldn't exist
expect(fs.existsSync(path.join(projectFolder, 'node_modules'))).toBe(false)
expect(fs.existsSync(path.join(projectFolder, '.git'))).toBe(false)
}, 15000)

it('Should create an boilerplate-project using an older version', () => {
const directory = path.join(__dirname, 'fixtures', 'empty-project')
const projectFolder = path.join(directory, projectName)
const version = '4.0.13'
const commandOutput = runChecklyCreateCli({
directory,
version,
promptsInjection: [projectName, 'boilerplate-project', false, false],
})

expectVersionAndName({ commandOutput, version, latestVersion, greeting })

const { status, stdout, stderr } = commandOutput

expect(stdout).toContain('Downloading example template...')
expect(stdout).toContain('Example template copied!')
expect(stdout).not.toContain('installing packages')
expect(stdout).not.toContain('Packages installed successfully')

// no git initialization message
expect(stdout).toContain('No worries. Just remember to install the dependencies after this setup')

expectCompleteCreation({ commandOutput, projectFolder })

expect(stderr).toBe('')
expect(status).toBe(0)

expect(fs.existsSync(path.join(projectFolder, 'package.json'))).toBe(true)
expect(fs.existsSync(path.join(projectFolder, 'checkly.config.ts'))).toBe(true)

// node_modules nor .git shouldn't exist
expect(fs.existsSync(path.join(projectFolder, 'node_modules'))).toBe(false)
expect(fs.existsSync(path.join(projectFolder, '.git'))).toBe(false)
}, 15000)

it('Should fail for already initiated project', () => {
const directory = path.join(__dirname, 'fixtures', 'initiated-project')
const commandOutput = runChecklyCreateCli({
directory,
promptsInjection: [projectName, 'advanced-project', false, false],
})

expectVersionAndName({ commandOutput, latestVersion, greeting })

const { status, stdout, stderr } = commandOutput

expect(stderr)
.toContain('It looks like you already have "__checks__" folder or "checkly.config.ts". ' +
'Please, remove them and try again.')

expect(stdout).not.toContain('Downloading example template...')
expect(stdout).not.toContain('Example template copied!')
expect(stdout).not.toContain('installing packages')
expect(stdout).not.toContain('Packages installed successfully')

expect(status).toBe(1)
}, 15000)

it('Should cancel command and show message', () => {
const directory = path.join(__dirname, 'fixtures', 'empty-project')
const commandOutput = runChecklyCreateCli({
directory,
promptsInjection: [new Error()],
})

expectVersionAndName({ commandOutput, latestVersion, greeting })
const { status, stderr } = commandOutput
expect(stderr).toContain('Bailing, hope to see you again soon!')
expect(status).toBe(2)
}, 15000)

it('Should create projects with all available templates (without installing dependencies)', () => {
const availableTemplates = PROJECT_TEMPLATES.map(t => t.value)

availableTemplates.forEach(template => {
const newProjectName = `${E2E_PROJECT_PREFIX}${uuidv4()}`
const directory = path.join(__dirname, 'fixtures', 'empty-project')
const projectFolder = path.join(directory, newProjectName)
const commandOutput = runChecklyCreateCli({
directory,
promptsInjection: [newProjectName, template, false, false],
})

expectVersionAndName({ commandOutput, latestVersion, greeting })

const { status, stdout, stderr } = commandOutput

expect(stdout).toContain('Downloading example template...')
expect(stdout).toContain('Example template copied!')
expect(stdout).not.toContain('installing packages')
expect(stdout).not.toContain('Packages installed successfully')

// no git initialization message
expect(stdout).toContain('No worries. Just remember to install the dependencies after this setup')

expectCompleteCreation({ commandOutput, projectFolder })

expect(stderr).toBe('')
expect(status).toBe(0)

expect(fs.existsSync(path.join(projectFolder, 'package.json'))).toBe(true)
expect(
fs.existsSync(path.join(projectFolder, 'checkly.config.ts')) ||
fs.existsSync(path.join(projectFolder, 'checkly.config.js')),
).toBe(true)

// node_modules nor .git shouldn't exist
expect(fs.existsSync(path.join(projectFolder, 'node_modules'))).toBe(false)
expect(fs.existsSync(path.join(projectFolder, '.git'))).toBe(false)
})
}, 30000)

it('Should create a project with --template argument', () => {
const directory = path.join(__dirname, 'fixtures', 'empty-project')
const projectFolder = path.join(directory, projectName)
const commandOutput = runChecklyCreateCli({
directory,
args: ['--template', 'boilerplate-project-js'],
promptsInjection: [projectName, false, false],
})

expectVersionAndName({ commandOutput, latestVersion, greeting })

const { status, stdout, stderr } = commandOutput

expect(stdout).toContain('Downloading example template...')
expect(stdout).toContain('Example template copied!')
expect(stdout).not.toContain('installing packages')
expect(stdout).not.toContain('Packages installed successfully')

// no git initialization message
expect(stdout).toContain('No worries. Just remember to install the dependencies after this setup')

expectCompleteCreation({ commandOutput, projectFolder })

expect(stderr).toBe('')
expect(status).toBe(0)

expect(fs.existsSync(path.join(projectFolder, 'package.json'))).toBe(true)
expect(fs.existsSync(path.join(projectFolder, 'checkly.config.js'))).toBe(true)
expect(fs.existsSync(path.join(projectFolder, '__checks__', 'api.check.js'))).toBe(true)

// node_modules nor .git shouldn't exist
expect(fs.existsSync(path.join(projectFolder, 'node_modules'))).toBe(false)
expect(fs.existsSync(path.join(projectFolder, '.git'))).toBe(false)
}, 15000)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is an empty project to test create-cli E2E tests.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ApiCheck, AssertionBuilder } from 'checkly/constructs'

const apiCheck = new ApiCheck('dummy-check', {
name: 'Dummy API check',
alertChannels: [],
degradedResponseTime: 10000,
maxResponseTime: 20000,
request: {
url: 'https://danube-web.shop/api/books',
method: 'GET',
followRedirects: true,
skipSSL: false,
assertions: [
AssertionBuilder.statusCode().equals(200),
AssertionBuilder.jsonBody('$[0].id').isNotNull(),
],
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { defineConfig } from 'checkly'

const config = defineConfig({
projectName: 'Dummy Project',
logicalId: 'e2e-test-project',
repoUrl: 'https://github.com/checkly/checkly-cli',
checks: {
frequency: 10,
locations: ['us-east-1', 'eu-west-1'],
tags: ['mac'],
runtimeId: '2023.02',
checkMatch: '**/__checks__/**/*.check.ts',
browserChecks: {
testMatch: '**/__checks__/**/*.spec.ts',
},
},
cli: {
runLocation: 'eu-west-1',
reporters: ['list'],
},
})

export default config
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "dummy-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"checkly": "latest",
"ts-node": "latest",
"typescript": "latest"
}
}
34 changes: 34 additions & 0 deletions packages/create-cli/e2e/run-create-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as path from 'path'
import * as childProcess from 'node:child_process'

const CHECKLY_PATH = path.resolve(path.dirname(__filename), '..', 'bin', 'run')

export function runChecklyCreateCli (options: {
directory?: string,
args?: string[],
env?: object,
version?: string,
promptsInjection?: (string | boolean | object)[],
timeout?: number,
}) {
const {
directory,
args = [],
env = {},
version,
promptsInjection = [],
timeout = 30000,
} = options
return childProcess.spawnSync(CHECKLY_PATH, args, {
env: {
PATH: process.env.PATH,
CHECKLY_CLI_VERSION: version,
CHECKLY_E2E_PROMPTS_INJECTIONS: promptsInjection?.length ? JSON.stringify(promptsInjection) : undefined,
...env,
},
cwd: directory ?? process.cwd(),
encoding: 'utf-8',
timeout,
shell: process.platform === 'win32',
})
}
Loading
Loading