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

fix: Fix unit tests #371

Merged
merged 6 commits into from Jun 6, 2023
Merged
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
8 changes: 8 additions & 0 deletions .github/workflows/check.yaml
Expand Up @@ -37,6 +37,14 @@ jobs:
-
name: Install Dependencies
run: npm install
-
name: Remove $HOME\.apify directory on Windows
if: runner.os == 'Windows'
run: Remove-Item -Recurse -Force $HOME\.apify
-
name: Remove ~/.apify directory on Linux
if: runner.os != 'Windows'
run: rm -rf ~/.apify
-
name: Run Tests
env:
Expand Down
28 changes: 28 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -4,8 +4,8 @@
"description": "Apify command-line interface helps you create, develop, build and run Apify actors, and manage the Apify cloud platform.",
"main": "index.js",
"scripts": {
"test": "mocha --timeout 180000 --recursive",
"test-python": "mocha --timeout 180000 --recursive --grep '\\[python\\]'",
"test": "cross-env APIFY_CLI_SKIP_UPDATE_CHECK=1 mocha --timeout 180000 --recursive",
"test-python": "npm run test -- --grep '\\[python\\]'",
"lint": "eslint src test",
"lint:fix": "eslint src test --fix",
"commands-md": "npm run manifest && oclif-dev readme",
Expand Down Expand Up @@ -94,6 +94,7 @@
"@oclif/test": "^2.1.0",
"chai": "^4.3.4",
"chai-match": "^1.1.1",
"cross-env": "^7.0.3",
"eslint": "^8.11.0",
"mocha": "^10.0.0",
"sinon": "^9.2.4"
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/init.js
@@ -1,4 +1,4 @@
const { checkLatestVersion } = require('../lib/version_check');
const { checkLatestVersion, SKIP_UPDATE_CHECK } = require('../lib/version_check');

/**
* This code'll be call before each commmand run
Expand All @@ -8,6 +8,9 @@ exports.default = async (params) => {
// This is not nessesary when you call the `--check-version` as the same command is called there.
if (['cv', 'check-version'].includes(params.id)) return;

// If the user has configured the `APIFY_CLI_SKIP_UPDATE_CHECK` env variable then skip the check.
if (SKIP_UPDATE_CHECK) return;

// Check package latest version
await checkLatestVersion();
};
6 changes: 6 additions & 0 deletions src/lib/version_check.js
Expand Up @@ -25,6 +25,11 @@ const UPDATE_COMMAND = {
[INSTALLATION_TYPE.NPM]: 'npm install -g apify-cli@latest',
};

const SKIP_UPDATE_CHECK = (
process.env.APIFY_CLI_SKIP_UPDATE_CHECK
&& !['0', 'false'].includes(process.env.APIFY_CLI_SKIP_UPDATE_CHECK.toLowerCase())
);

/**
* Detect through which package manager the Apify CLI was installed.
* @returns {INSTALLATION_TYPE} The installation type of the CLI.
Expand Down Expand Up @@ -115,4 +120,5 @@ const checkLatestVersion = async (enforeUpdate = false) => {
module.exports = {
checkLatestVersion,
getLatestNpmVersion,
SKIP_UPDATE_CHECK,
};
15 changes: 8 additions & 7 deletions test/commands/call.js
Expand Up @@ -48,13 +48,14 @@ const waitForBuildToFinishWithTimeout = async (client, buildId, timeoutSecs = 60

let actorId;
describe('apify call', () => {
before(async function () {
let skipAfterHook = false;
before(async () => {
if (fs.existsSync(GLOBAL_CONFIGS_FOLDER)) {
// Skip tests if user used CLI on local, it can break local environment!
console.warn(`Test was skipped as directory ${GLOBAL_CONFIGS_FOLDER} exists!`);
this.skip();
return;
// Tests could break local environment if user is already logged in
skipAfterHook = true;
throw new Error(`Cannot run tests, directory ${GLOBAL_CONFIGS_FOLDER} exists! Run "apify logout" to fix this.`);
}

const { username } = await testUserClient.user('me').get();
await command.run(['login', '--token', TEST_USER_TOKEN]);
await command.run(['create', ACTOR_NAME, '--template', 'project_empty']);
Expand All @@ -73,7 +74,7 @@ describe('apify call', () => {

fs.writeFileSync(inputFile, JSON.stringify(EXPECTED_INPUT), { flag: 'w' });

await command.run(['push']);
await command.run(['push', '--no-prompt']);

actorId = `${username}/${ACTOR_NAME}`;

Expand All @@ -98,7 +99,7 @@ describe('apify call', () => {
});

after(async () => {
if (!actorId) return; // Test was skipped.
if (skipAfterHook) return;
await testUserClient.actor(actorId).delete();
process.chdir('../');
if (fs.existsSync(ACTOR_NAME)) await rimrafPromised(ACTOR_NAME);
Expand Down
20 changes: 12 additions & 8 deletions test/commands/info.js
Expand Up @@ -7,39 +7,43 @@ const { GLOBAL_CONFIGS_FOLDER, AUTH_FILE_PATH } = require('../../src/lib/consts'
const { TEST_USER_TOKEN } = require('./config');

describe('apify info', () => {
before(function () {
let skipAfterHook = false;
before(() => {
if (fs.existsSync(GLOBAL_CONFIGS_FOLDER)) {
// Skip tests if user used CLI on local, it can break local environment!
console.warn(`Test was skipped as directory ${GLOBAL_CONFIGS_FOLDER} exists!`);
this.skip();
// Tests could break local environment if user is already logged in
skipAfterHook = true;
throw new Error(`Cannot run tests, directory ${GLOBAL_CONFIGS_FOLDER} exists! Run "apify logout" to fix this.`);
}
});

beforeEach(() => {
sinon.spy(console, 'log');
});

it('should end with Error', async () => {
it('should end with Error when not logged in', async () => {
try {
await command.run(['info']);
} catch (e) {
expect(e).to.be.an('error');
}
});

it('should work', async () => {
it('should work when logged in', async () => {
await command.run(['login', '--token', TEST_USER_TOKEN]);
await command.run(['info']);

const userInfoFromConfig = loadJson.sync(AUTH_FILE_PATH);

expect(console.log.callCount).to.eql(3);
expect(console.log.args[2][0]).to.include(userInfoFromConfig.id);

await command.run(['logout']);
});

afterEach(() => {
console.log.restore();
});

after(async () => {
if (skipAfterHook) return;
await command.run(['logout']);
});
});
18 changes: 12 additions & 6 deletions test/commands/log_in_out.js
Expand Up @@ -8,26 +8,27 @@ const { GLOBAL_CONFIGS_FOLDER, AUTH_FILE_PATH } = require('../../src/lib/consts'
const { testUserClient, TEST_USER_TOKEN, TEST_USER_BAD_TOKEN } = require('./config');

describe('apify login and logout', () => {
before(function () {
let skipAfterHook = false;
before(() => {
if (fs.existsSync(GLOBAL_CONFIGS_FOLDER)) {
// Skip tests if user used CLI on local, it can break local environment!
console.warn(`Test was skipped as directory ${GLOBAL_CONFIGS_FOLDER} exists!`);
this.skip();
// Tests could break local environment if user is already logged in
skipAfterHook = true;
throw new Error(`Cannot run tests, directory ${GLOBAL_CONFIGS_FOLDER} exists! Run "apify logout" to fix this.`);
}
});

beforeEach(() => {
sinon.spy(console, 'log');
});

it('should end with Error', async () => {
it('should end with Error with bad token', async () => {
await command.run(['login', '--token', TEST_USER_BAD_TOKEN]);

expect(console.log.callCount).to.eql(1);
expect(console.log.args[0][0]).to.include('Error:');
});

it('should work', async () => {
it('should work with correct token', async () => {
await command.run(['login', '--token', TEST_USER_TOKEN]);

const expectedUserInfo = Object.assign(await testUserClient.user('me').get(), { token: TEST_USER_TOKEN });
Expand All @@ -48,4 +49,9 @@ describe('apify login and logout', () => {
afterEach(() => {
console.log.restore();
});

after(() => {
if (skipAfterHook) return;
fs.rmSync(GLOBAL_CONFIGS_FOLDER, { recursive: true, force: true });
});
});