Skip to content

Commit

Permalink
test: add new test cases for --encoding option of the export command (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hung-cybo authored Dec 8, 2023
1 parent 8dcc6a5 commit 745d325
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 13 deletions.
33 changes: 33 additions & 0 deletions features/export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,39 @@ Feature: cli-kintone export command
Then I should get the exit code is non-zero
And The output error message should match with the pattern: "\[403] \[CB_NO02] No privilege to proceed"

Scenario: CliKintoneTest-111 Should return the record contents when exporting the record with --encoding option is utf8.
Given The app "app_for_export" has no records
And The app "app_for_export" has some records as below:
| Text | Number |
| レコード番号 | 10 |
And Load app ID of the app "app_for_export" as env var: "APP_ID"
And Load app token of the app "app_for_export" with exact permissions "view" as env var: "API_TOKEN"
When I run the command with args "record export --base-url $$TEST_KINTONE_BASE_URL --app $APP_ID --api-token $API_TOKEN --encoding utf8"
Then I should get the exit code is zero
And The output message with "utf8" encoded should match with the data below:
| Record_number | Text | Number |
| \d+ | レコード番号 | 10 |

Scenario: CliKintoneTest-112 Should return the record contents when exporting the record with --encoding option is sjis.
Given The app "app_for_export" has no records
And The app "app_for_export" has some records as below:
| Text | Number |
| 作成日時 | 10 |
And Load app ID of the app "app_for_export" as env var: "APP_ID"
And Load app token of the app "app_for_export" with exact permissions "view" as env var: "API_TOKEN"
When I run the command with args "record export --base-url $$TEST_KINTONE_BASE_URL --app $APP_ID --api-token $API_TOKEN --encoding sjis"
Then I should get the exit code is zero
And The output message with "sjis" encoded should match with the data below:
| Record_number | Text | Number |
| \d+ | 作成日時 | 10 |

Scenario: CliKintoneTest-113 Should return the error message when exporting the record with an unsupported character encoding.
Given Load app ID of the app "app_for_export" as env var: "APP_ID"
And Load app token of the app "app_for_export" with exact permissions "view" as env var: "API_TOKEN"
When I run the command with args "record export --base-url $$TEST_KINTONE_BASE_URL --app $APP_ID --api-token $API_TOKEN --encoding unsupported_encoding"
Then I should get the exit code is non-zero
And The output error message should match with the pattern: "Argument: encoding, Given: \"unsupported_encoding\", Choices: \"utf8\", \"sjis\""

Scenario: CliKintoneTest-124 Should return the record contents successfully, including table data, when exporting the records
Given Load app ID of the app "app_for_export_table" as env var: "APP_ID"
And Load app token of the app "app_for_export_table" with exact permissions "view" as env var: "API_TOKEN"
Expand Down
10 changes: 5 additions & 5 deletions features/step_definitions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,26 @@ When("I run the command with args {string}", function (args: string) {
});

Then("I should get the exit code is non-zero", function () {
assert.notEqual(this.response.status, 0, this.response.stderr);
assert.notEqual(this.response.status, 0, this.response.stderr.toString());
});

Then("I should get the exit code is zero", function () {
assert.equal(this.response.status, 0, this.response.stderr);
assert.equal(this.response.status, 0, this.response.stderr.toString());
});

Then(
"The output error message should match with the pattern: {string}",
function (errorMessage: string) {
const reg = new RegExp(errorMessage);
assert.match(this.response.stderr, reg);
assert.match(this.response.stderr.toString(), reg);
},
);

Then(
"The output message should match with the pattern: {string}",
function (message: string) {
const reg = new RegExp(message);
assert.match(this.response.stdout, reg);
assert.match(this.response.stdout.toString(), reg);
},
);

Expand All @@ -197,7 +197,7 @@ Then(
const values = record
.map((field: string) => (field ? `"${field}"` : ""))
.join(",");
assert.match(this.response.stdout, new RegExp(`${values}`));
assert.match(this.response.stdout.toString(), new RegExp(`${values}`));
});
},
);
Expand Down
19 changes: 17 additions & 2 deletions features/step_definitions/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as assert from "assert";
import { Given, Then } from "../utils/world";
import fs from "fs";
import path from "path";
import iconv from "iconv-lite";

Given("I have a directory {string}", function (directory: string) {
fs.mkdirSync(path.join(this.workingDir, directory));
Expand Down Expand Up @@ -52,7 +53,7 @@ Then(
});
const reg = new RegExp(matchStr);
assert.match(
this.response.stdout,
this.response.stdout.toString(),
reg,
`The output message does not match the data in the order.\nExpected: \n${JSON.stringify(
records,
Expand All @@ -79,7 +80,21 @@ Then(
return `"${field}"`;
})
.join(",");
assert.match(this.response.stdout, new RegExp(`${values}`));
assert.match(this.response.stdout.toString(), new RegExp(`${values}`));
});
},
);

Then(
"The output message with {string} encoded should match with the data below:",
async function (encoding, table) {
const decodedOutputMsg = iconv.decode(this.response.stdout, encoding);
const records = this.replacePlaceholdersInRawDataTables(table.raw());
records.forEach((record: string[]) => {
const values = record
.map((field: string) => (field ? `"${field}"` : ""))
.join(",");
assert.match(decodedOutputMsg, new RegExp(`${values}`));
});
},
);
4 changes: 2 additions & 2 deletions features/step_definitions/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Then(
const values = record
.map((field: string) => (field ? `"${field}"` : ""))
.join(",");
assert.match(this.response.stdout, new RegExp(`${values}`));
assert.match(this.response.stdout.toString(), new RegExp(`${values}`));
});
},
);
Expand Down Expand Up @@ -93,7 +93,7 @@ Then(
return `"${field}"`;
})
.join(",");
assert.match(this.response.stdout, new RegExp(`${values}`));
assert.match(this.response.stdout.toString(), new RegExp(`${values}`));
});
},
);
Expand Down
2 changes: 1 addition & 1 deletion features/step_definitions/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ Then(
"I should get the version formatted in {string}",
function (versionFormat: string) {
const reg = new RegExp(versionFormat);
assert.match(this.response.stdout, reg);
assert.match(this.response.stdout.toString(), reg);
},
);
6 changes: 4 additions & 2 deletions features/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export const execCliKintoneSync = (
);

const response = spawnSync(getCliKintoneBinary(), cleanedArgs, {
encoding: "utf-8",
env: options?.env ?? {},
cwd: options?.cwd ?? process.cwd(),
});
Expand Down Expand Up @@ -144,7 +143,10 @@ export const getRecordNumbers = (appId: string, apiToken: string): string[] => {
throw new Error(`Getting records failed. Error: \n${response.stderr}`);
}

const recordNumbers = response.stdout.replace(/"/g, "").split("\n");
const recordNumbers = response.stdout
.toString()
.replace(/"/g, "")
.split("\n");
recordNumbers.shift();

return recordNumbers.filter((recordNumber) => recordNumber.length > 0);
Expand Down
2 changes: 1 addition & 1 deletion features/utils/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class OurWorld extends World {
public replacements: Replacements = {};
private _workingDir?: string;
private _credentials?: Credentials;
private _response?: SpawnSyncReturns<string>;
private _response?: SpawnSyncReturns<Buffer>;

public get response() {
if (this._response === undefined) {
Expand Down

0 comments on commit 745d325

Please sign in to comment.