Skip to content

Commit

Permalink
test: new logic based on node stdio
Browse files Browse the repository at this point in the history
  • Loading branch information
pcheremu committed Jan 31, 2024
1 parent a69912f commit 68a13d5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 92 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test-cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: npm

- name: Install dependencies
- name: Install dependencies and build
run: |
npm ci
npm i zksync-cli
npm ci && npm run build
zksync-cli --version
- name: Run tests
env:
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ In the meantime, you can test the code manually by running the code in [developm
### CLI tests

To run CLI tests for zksync-cli do the following:
1. Go to [test](`./test`) folder
2. Run `npm i` and install zksync-cli `npm i zksync-cli`
1. Make preparation step `1` and `2` from `Building for production`, or just run `npm i && npm run build`
3. Make sure you have [Docker](https://docs.docker.com/engine/install/) on your system.
4. Rename `.env.example` to `.env` in `./test/src/` dir and add your wallet private key to this file. F.e.: It may looks like `E2E_TESTNET_PK=012345...abcdef`
5. Run `npm run test`
Expand Down
66 changes: 0 additions & 66 deletions package-lock.json

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

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,15 @@
"@types/inquirer": "^8.0.2",
"@types/jest": "^29.5.11",
"@types/node": "^18.17.12",
"@types/shelljs": "^0.8.15",
"@types/update-notifier": "^6.0.8",
"husky": "^8.0.3",
"jest": "^29.7.0",
"lint-staged": "^14.0.1",
"semantic-release": "^22.0.8",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.1",
"tsc-files": "^1.1.4",
"typescript": "^5.2.2",
"jest": "^29.7.0",
"shelljs": "^0.8.5",
"ts-jest": "^29.1.2"
"typescript": "^5.2.2"
},
"publishConfig": {
"access": "public"
Expand Down
8 changes: 4 additions & 4 deletions test/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ describe("Check version of package", () => {
it("npx zksync-cli -V", () => {
const command = "npx zksync-cli -V";
const result = executeCommand(command);
expect(result.output).toMatch(/(^\d+\.\d+\.\d+$)/i);
expect(result.output).toMatch(/(^\d+\.\d+\.\d+(-\w+)?$)/i);
expect(result.exitCode).toBe(0);
});

it("npx zksync-cli --version", () => {
const command = "npx zksync-cli --version";
const result = executeCommand(command);
expect(result.output).toMatch(/(^\d+\.\d+\.\d+$)/i);
expect(result.output).toMatch(/(^\d+\.\d+\.\d+(-\w+)?$)/i);
expect(result.exitCode).toBe(0);
});

it("Negative: npx zksync-cli --wersion", () => {
const command = "npx zksync-cli --wersion";
const result = executeCommand(command);
expect(result.output).not.toMatch(/(^\d+\.\d+\.\d+$)/i);
expect(result.output).not.toMatch(/(^\d+\.\d+\.\d+(-\w+)?$)/i);
expect(result.exitCode).toBe(1);
});
});
Expand Down Expand Up @@ -160,7 +160,7 @@ describe("User can call write method from deployed contract on network", () => {
it("npx zksync-cli contract write", () => {
let optionalRedirection = "> /dev/null";
if (process.platform === "win32") {
optionalRedirection = " > nul ";
optionalRedirection = "";
}
const command = `npx zksync-cli contract write --chain zksync-sepolia\
--contract ${contracts.sepoliaTestnet} --method "setGreeting(string _greeting) "\
Expand Down
28 changes: 16 additions & 12 deletions test/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import * as shell from "shelljs";
import { spawnSync } from "child_process";

export interface CommandResult {
output: string;
exitCode: number;
}

export const executeCommand = (command: string): CommandResult => {
const result = shell.exec(command, { async: false });
return {
exitCode: result.code,
output: result.stdout.trim() || result.stderr.trim(),
};
};
export function executeCommand(command: string) {
const result = spawnSync(command, { encoding: 'utf-8', shell: true, stdio: 'pipe' });

if (result.error) {
return {
output: '' || result.stderr.trim(),
exitCode: result.status || 1, error: result.error
};
} else {
return {
output: result.stdout.trim() || result.stderr.trim(),
exitCode: result.status || 0
};
}
}

0 comments on commit 68a13d5

Please sign in to comment.