From c14252d907855cee310db7b57fe37a776fd4424a Mon Sep 17 00:00:00 2001 From: Igor Savin Date: Tue, 28 Jan 2020 02:28:40 +0100 Subject: [PATCH] Refactor more tests to use cli-testlab (#3640) --- lib/util/parse-connection.js | 4 +- test/cli/cli-test-utils.js | 9 ++-- test/cli/migrate.spec.js | 59 ++++++++++++++++++++++++++ test/cli/seed.spec.js | 60 ++++++++++++++++++++++++++ test/index.js | 4 +- test/jake/Jakefile | 4 +- test/jake/jakelib/migrate-test.js | 31 -------------- test/jake/jakelib/seed-test.js | 70 ------------------------------- 8 files changed, 132 insertions(+), 109 deletions(-) create mode 100644 test/cli/migrate.spec.js create mode 100644 test/cli/seed.spec.js delete mode 100644 test/jake/jakelib/seed-test.js diff --git a/lib/util/parse-connection.js b/lib/util/parse-connection.js index feab65cd30..e9ef7b379f 100644 --- a/lib/util/parse-connection.js +++ b/lib/util/parse-connection.js @@ -1,11 +1,13 @@ const url = require('url'); const { parse } = require('pg-connection-string'); const parsePG = parse; +const isWindows = process && process.platform && process.platform === 'win32'; module.exports = function parseConnectionString(str) { const parsed = url.parse(str, true); let { protocol } = parsed; - if (protocol === null) { + const isDriveLetter = isWindows && protocol.length === 2; + if (protocol === null || isDriveLetter) { return { client: 'sqlite3', connection: { diff --git a/test/cli/cli-test-utils.js b/test/cli/cli-test-utils.js index f95b11c2ad..39b69b0b5b 100644 --- a/test/cli/cli-test-utils.js +++ b/test/cli/cli-test-utils.js @@ -63,10 +63,12 @@ function expectContentMatchesStub(stubPath, globPath, fileHelper) { expect(content).equals(stubContent); } +function getRootDir() { + return path.resolve(__dirname, '../jake-util'); +} + function setupFileHelper() { - const fileHelper = new FileTestHelper( - path.resolve(__dirname, '../jake-util') - ); + const fileHelper = new FileTestHelper(getRootDir()); fileHelper.deleteFile('test.sqlite3'); fileHelper.registerForCleanup('test.sqlite3'); @@ -75,6 +77,7 @@ function setupFileHelper() { module.exports = { expectContentMatchesStub, + getRootDir, migrationStubOptionSetup, seedStubOptionSetup, setupFileHelper, diff --git a/test/cli/migrate.spec.js b/test/cli/migrate.spec.js new file mode 100644 index 0000000000..fbc5e9aa75 --- /dev/null +++ b/test/cli/migrate.spec.js @@ -0,0 +1,59 @@ +'use strict'; + +const path = require('path'); +const sqlite3 = require('sqlite3'); +const { expect } = require('chai'); +const { execCommand } = require('cli-testlab'); +const { getRootDir, setupFileHelper } = require('./cli-test-utils'); + +const KNEX = path.normalize(__dirname + '/../../bin/cli.js'); + +describe('migrate:latest', () => { + before(() => { + process.env.KNEX_PATH = '../knex.js'; + }); + + let fileHelper; + const rootDir = getRootDir(); + const dbPath = path.resolve(rootDir, 'db'); + beforeEach(() => { + fileHelper = setupFileHelper(); + fileHelper.registerForCleanup(dbPath); + }); + afterEach(() => { + fileHelper.cleanup(); + }); + + it('Run migrations', async () => { + fileHelper.createFile( + 'migrations/000_create_rule_table.js', + ` + exports.up = (knex)=> knex.schema.createTable('rules', (table)=> { + table.string('name'); + }); + exports.down = (knex)=> knex.schema.dropTable('rules'); + `, + { willBeCleanedUp: true, isPathAbsolute: false } + ); + + expect(fileHelper.fileExists(dbPath)).to.equal(false); + await execCommand(`node ${KNEX} migrate:latest \ + --client=sqlite3 --connection=${dbPath} \ + --migrations-directory=${rootDir}/migrations \ + create_rule_table`); + expect(fileHelper.fileExists(dbPath)).to.equal(true); + const db = await new sqlite3.Database(dbPath); + + const getPromise = new Promise((resolve, reject) => { + db.get('SELECT name FROM knex_migrations', {}, (err, row) => { + if (err) { + reject(err); + } + resolve(row); + }); + }); + const row = await getPromise; + expect(row.name).to.equal('000_create_rule_table.js'); + db.close(); + }); +}); diff --git a/test/cli/seed.spec.js b/test/cli/seed.spec.js new file mode 100644 index 0000000000..cd8ea63c7e --- /dev/null +++ b/test/cli/seed.spec.js @@ -0,0 +1,60 @@ +'use strict'; + +const path = require('path'); +const { execCommand } = require('cli-testlab'); + +const KNEX = path.normalize(__dirname + '/../../bin/cli.js'); + +describe('seed:run', () => { + before(() => { + process.env.KNEX_PATH = '../knex.js'; + }); + + it('prints non verbose logs', () => { + return execCommand( + `node ${KNEX} seed:run --knexfile=test/jake-util/seeds-knexfile.js`, + { + expectedOutput: 'Ran 2 seed files', + notExpectedOutput: ['first.js', 'second.js'], + } + ); + }); + + it('prints verbose logs', () => { + return execCommand( + `node ${KNEX} seed:run --knexfile=test/jake-util/seeds-knexfile.js --verbose`, + { + expectedOutput: ['Ran 2 seed files', 'first.js', 'second.js'], + } + ); + }); + + it('runs specific file', () => { + return execCommand( + `node ${KNEX} seed:run --knexfile=test/jake-util/seeds-knexfile.js --specific=second.js`, + { + expectedOutput: 'Ran 1 seed files', + notExpectedOutput: ['first.js', 'second.js'], + } + ); + }); + + it('handles seeding errors correctly', () => { + return execCommand( + `node ${KNEX} seed:run --knexfile=test/jake-util/seeds-error-knexfile.js`, + { + expectedErrorMessage: ['Error while executing', 'seeds.js', 'Boom'], + } + ); + }); + + it('seed:run runs "esm" files', () => { + return execCommand( + `node ${KNEX} --esm seed:run --knexfile=test/jake-util/knexfile-esm/knexfile.js`, + { + expectedOutput: 'Ran 1 seed files', + notExpectedOutput: ['first.js', 'second.js'], + } + ); + }); +}); diff --git a/test/index.js b/test/index.js index 37331aa2bc..61afbb76ef 100644 --- a/test/index.js +++ b/test/index.js @@ -89,9 +89,11 @@ if (config.sqlite3) { describe('CLI tests', function() { this.timeout(process.env.KNEX_TEST_TIMEOUT || 5000); + require('./cli/help.spec'); require('./cli/knexfile-test.spec'); + require('./cli/migrate.spec'); require('./cli/migrate-make.spec'); + require('./cli/seed.spec'); require('./cli/seed-make.spec'); require('./cli/version.spec'); - require('./cli/help.spec'); }); diff --git a/test/jake/Jakefile b/test/jake/Jakefile index 364974a093..e3e528d42a 100644 --- a/test/jake/Jakefile +++ b/test/jake/Jakefile @@ -4,11 +4,9 @@ /* eslint-disable no-console */ const migrateTests = require('./jakelib/migrate-test').taskList; -const seedTests = require('./jakelib/seed-test').taskList; const tests = [ - ...migrateTests, - ...seedTests + ...migrateTests ]; task('default', tests, () => { diff --git a/test/jake/jakelib/migrate-test.js b/test/jake/jakelib/migrate-test.js index d61b3c4b3c..a9240d5236 100644 --- a/test/jake/jakelib/migrate-test.js +++ b/test/jake/jakelib/migrate-test.js @@ -82,37 +82,6 @@ test('Create a migration file without client passed', (temp) => ) )); -test('Run migrations', (temp) => - new Promise((resolve, reject) => - fs.writeFile( - temp + '/migrations/000_create_rule_table.js', - ` - exports.up = (knex)=> knex.schema.createTable('rules', (table)=> { - table.string('name'); - }); - exports.down = (knex)=> knex.schema.dropTable('rules'); - `, - (err) => (err ? reject(err) : resolve()) - ) - ) - .then(() => - assertExec(`${KNEX} migrate:latest \ - --client=sqlite3 --connection=${temp}/db \ - --migrations-directory=${temp}/migrations \ - create_rule_table`) - ) - .then(() => assertExec(`ls ${temp}/db`, 'Find the database file')) - .then(() => new sqlite3.Database(temp + '/db')) - .then( - (db) => - new Promise((resolve, reject) => - db.get('SELECT name FROM knex_migrations', function(err, row) { - err ? reject(err) : resolve(row); - }) - ) - ) - .then((row) => assert.equal(row.name, '000_create_rule_table.js'))); - test('run migrations without knexfile and with --migrations-table-name', (temp) => assertExec(`${KNEX} migrate:latest \ --client=sqlite3 --connection=${temp}/db \ diff --git a/test/jake/jakelib/seed-test.js b/test/jake/jakelib/seed-test.js deleted file mode 100644 index b7e15a2f82..0000000000 --- a/test/jake/jakelib/seed-test.js +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env jake -'use strict'; -/* eslint-disable no-undef */ - -const path = require('path'); -const { - assertExec, - assertExecError, - test, -} = require('../../jake-util/helpers/migration-test-helper'); -const { assert } = require('chai'); - -const KNEX = path.normalize(__dirname + '/../../../bin/cli.js'); - -const taskList = []; -/* * * TESTS * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -test(taskList, 'seed:run prints non verbose logs', (temp) => { - return assertExec( - `node ${KNEX} seed:run --knexfile=test/jake-util/seeds-knexfile.js --knexpath=../knex.js` - ).then(({ stdout }) => { - assert.include(stdout, 'Ran 2 seed files'); - assert.notInclude(stdout, 'first.js'); - assert.notInclude(stdout, 'second.js'); - }); -}); - -test(taskList, 'seed:run prints verbose logs', (temp) => { - return assertExec( - `node ${KNEX} seed:run --knexfile=test/jake-util/seeds-knexfile.js --knexpath=../knex.js --verbose` - ).then(({ stdout }) => { - assert.include(stdout, 'Ran 2 seed files'); - assert.include(stdout, 'first.js'); - assert.include(stdout, 'second.js'); - }); -}); - -test(taskList, 'seed:run runs specific file', () => { - return assertExec( - `node ${KNEX} seed:run --knexfile=test/jake-util/seeds-knexfile.js --knexpath=../knex.js --specific=second.js` - ).then(({ stdout }) => { - assert.include(stdout, 'Ran 1 seed files'); - assert.notInclude(stdout, 'first.js'); - assert.notInclude(stdout, 'second.js'); - }); -}); - -test(taskList, 'Handles seeding errors correctly', (temp) => { - return assertExecError( - `node ${KNEX} seed:run --knexfile=test/jake-util/seeds-error-knexfile.js --knexpath=../knex.js` - ).then((err) => { - assert.include(err, 'Error while executing'); - assert.include(err, 'seeds.js'); - assert.include(err, 'Boom'); - }); -}); - -test(taskList, 'seed:run runs "esm" files', () => { - return assertExec( - `node ${KNEX} --esm seed:run --knexfile=test/jake-util/knexfile-esm/knexfile.js` - ).then(({ stdout }) => { - assert.include(stdout, 'Ran 1 seed files'); - assert.notInclude(stdout, 'first.js'); - assert.notInclude(stdout, 'second.js'); - }); -}); - -module.exports = { - taskList, -};