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

Do not overwrite cwd if knexfile and cwd is passed #6060

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/cli.js
Expand Up @@ -87,7 +87,7 @@ async function initKnex(env, opts, useDefaultClientIfNotSpecified) {
function invoke() {
const filetypes = ['js', 'mjs', 'coffee', 'ts', 'eg', 'ls'];

const cwd = argv.knexfile
const cwd = argv.knexfile && !argv.cwd
? path.dirname(path.resolve(argv.knexfile))
: process.cwd();

Expand Down
34 changes: 27 additions & 7 deletions test/cli/knexfile-test.spec.js
Expand Up @@ -164,23 +164,43 @@ module.exports = {
}
);
});

it("does not change the process's cwd", function () {
return execCommand(
`node ${KNEX} migrate:latest --cwd=test/jake-util/knexfile --knexfile=subdir/knexfile.js`,
{
notExpectedOutput: 'Working directory changed to',
}
);
});
});

context('and --knexfile is an absolute path', function () {
// Notice: the Knexfile is using Typescript. This means that Knex
// is pre-loading the appropriate Typescript modules before loading
// the Knexfile.
const knexfile = path.resolve(
'test/jake-util/knexfile-ts/custom-config.ts'
);
const command = `node ${KNEX} migrate:latest --cwd=test/jake-util/knexfile --knexfile=${knexfile}`;

it('uses the indicated knexfile', function () {
// Notice: the Knexfile is using Typescript. This means that Knex
// is pre-loading the appropriate Typescript modules before loading
// the Knexfile.
const knexfile = path.resolve(
'test/jake-util/knexfile-ts/custom-config.ts'
);
return execCommand(
`node ${KNEX} migrate:latest --cwd=test/jake-util/knexfile --knexfile=${knexfile}`,
command,
{
expectedOutput: 'Batch 1 run: 4 migrations',
}
);
});

it("does not change the process's cwd", function () {
return execCommand(
command,
{
notExpectedOutput: 'Working directory changed to',
}
);
});
});
});

Expand Down
6 changes: 3 additions & 3 deletions test/jake-util/knexfile-ts/custom-config.ts
@@ -1,13 +1,13 @@
export const client = 'sqlite3';

export const connection = {
filename: '../test.sqlite3',
filename: __dirname + '/../test.sqlite3',
};

export const migrations = {
directory: './knexfile_migrations',
directory: __dirname + '/knexfile_migrations',
};

export const seeds = {
directory: './knexfile_seeds',
directory: __dirname + '/../knexfile_seeds',
};
12 changes: 12 additions & 0 deletions test/jake-util/knexfile/subdir/knexfile.js
@@ -0,0 +1,12 @@
module.exports = {
client: 'sqlite3',
connection: {
filename: __dirname + '/../../test.sqlite3',
},
migrations: {
directory: __dirname + '/../../knexfile_migrations',
},
seeds: {
directory: __dirname + '/../../knexfile_seeds',
},
}