From 304a574515f93fc9f34f2e292e1d745cd8e18038 Mon Sep 17 00:00:00 2001 From: hung-nguyen Date: Thu, 23 Nov 2023 17:07:12 +0700 Subject: [PATCH 1/4] test: add new test cases CliKintoneTest-62-63-64 --- features/import.feature | 39 +++++++++++++++++++++++++++++ features/step_definitions/import.ts | 15 ++++++++++- features/utils/helper.ts | 35 +++++++++++++++++++------- features/utils/world.ts | 15 ++++++++--- 4 files changed, 91 insertions(+), 13 deletions(-) diff --git a/features/import.feature b/features/import.feature index 99b57f4771..9516d562fd 100644 --- a/features/import.feature +++ b/features/import.feature @@ -389,3 +389,42 @@ Feature: cli-kintone import command When I run the command with args "record import --base-url $$TEST_KINTONE_BASE_URL --app $APP_ID --api-token $API_TOKEN --fields Text,Non_Existent_Field_Code --file-path CliKintoneTest-53.csv" Then I should get the exit code is non-zero And The output error message should match with the pattern: "Error: The specified field \"Non_Existent_Field_Code\" does not exist on the app" + + Scenario: CliKintoneTest-62 Should import the records successfully with --encoding option is utf8 + Given The app "app_for_import" has no records + And The csv file "CliKintoneTest-62.csv" with "utf8" content as below: + | Text | Number | + | レコード番号 | 10 | + And Load app ID of the app "app_for_import" as env var: "APP_ID" + And Load app token of the app "app_for_import" with exact permissions "add" as env var: "API_TOKEN" + When I run the command with args "record import --base-url $$TEST_KINTONE_BASE_URL --app $APP_ID --api-token $API_TOKEN --encoding utf8 --file-path CliKintoneTest-62.csv" + Then I should get the exit code is zero + And The app "app_for_import" should has records as below: + | Text | Number | + | レコード番号 | 10 | + + Scenario: CliKintoneTest-63 Should import the records successfully with --encoding option is sjis + Given The app "app_for_import" has no records + And The csv file "CliKintoneTest-63.csv" with "sjis" content as below: + | Text | Number | + | 作成日時 | 10 | + And Load app ID of the app "app_for_import" as env var: "APP_ID" + And Load app token of the app "app_for_import" with exact permissions "add" as env var: "API_TOKEN" + When I run the command with args "record import --base-url $$TEST_KINTONE_BASE_URL --app $APP_ID --api-token $API_TOKEN --encoding sjis --file-path CliKintoneTest-63.csv" + Then I should get the exit code is zero + And The app "app_for_import" should has records as below: + | Text | Number | + | 作成日時 | 10 | + + Scenario: CliKintoneTest-64 Should return the error message when importing records with an unsupported character code. + Given The app "app_for_import" has no records + And The csv file "CliKintoneTest-64.csv" with content as below: + | Text | Number | + | Alice | 10 | + | Bob | 20 | + | Jenny | 30 | + And Load app ID of the app "app_for_import" as env var: "APP_ID" + And Load app token of the app "app_for_import" with exact permissions "add" as env var: "API_TOKEN" + When I run the command with args "record import --base-url $$TEST_KINTONE_BASE_URL --app $APP_ID --api-token $API_TOKEN --encoding unsupported_character_code --file-path CliKintoneTest-64.csv" + Then I should get the exit code is non-zero + And The output error message should match with the pattern: "Argument: encoding, Given: \"unsupported_character_code\", Choices: \"utf8\", \"sjis\"" diff --git a/features/step_definitions/import.ts b/features/step_definitions/import.ts index 6fafd7a445..969cbed5d6 100644 --- a/features/step_definitions/import.ts +++ b/features/step_definitions/import.ts @@ -1,11 +1,24 @@ import * as assert from "assert"; import { Given, Then } from "../utils/world"; import fs from "fs"; +import type { SupportedEncoding } from "../utils/helper"; +import { SUPPORTED_ENCODING } from "../utils/helper"; Given( "The csv file {string} with content as below:", async function (filePath: string, table) { - await this.generateCsvFile(table.raw(), filePath); + await this.generateCsvFile(table.raw(), { filePath }); + }, +); + +Given( + "The csv file {string} with {string} content as below:", + async function (filePath: string, encoding: SupportedEncoding, table) { + if (!SUPPORTED_ENCODING.includes(encoding)) { + throw new Error(`The encoding ${encoding} is not supported`); + } + + await this.generateCsvFile(table.raw(), { filePath, encoding }); }, ); diff --git a/features/utils/helper.ts b/features/utils/helper.ts index a70214cea5..db66145b81 100644 --- a/features/utils/helper.ts +++ b/features/utils/helper.ts @@ -1,6 +1,11 @@ import { spawnSync } from "child_process"; import path from "path"; import fs from "fs/promises"; +import iconv from "iconv-lite"; + +export const SUPPORTED_ENCODING = ["utf8", "sjis"]; + +export type SupportedEncoding = (typeof SUPPORTED_ENCODING)[number]; export const execCliKintoneSync = ( args: string, @@ -68,13 +73,13 @@ const inputEnvReplacer = (envVars: { [key: string]: string } | undefined) => { }; export const generateCsvFile = async ( - inputCsvObject: string[][], - options: { baseDir?: string; destFilePath?: string }, + csvContent: string, + options: { + baseDir?: string; + destFilePath?: string; + encoding?: SupportedEncoding; + }, ): Promise => { - const csvContent = inputCsvObject - .map((row) => row.map((field) => `"${field}"`).join(",")) - .join("\n"); - let filePath = options.destFilePath; if (filePath) { filePath = options.baseDir @@ -89,7 +94,7 @@ export const generateCsvFile = async ( filePath = path.join(tempDir, "records.csv"); } - await fs.writeFile(filePath, csvContent); + await _writeFile(csvContent, filePath, { encoding: options.encoding }); return filePath; }; @@ -97,17 +102,29 @@ export const generateCsvFile = async ( export const generateFile = async ( content: string, filePath: string, - options: { baseDir?: string }, + options: { baseDir?: string; encoding?: SupportedEncoding }, ): Promise => { const actualFilePath = options.baseDir ? path.join(options.baseDir, filePath) : filePath; await fs.mkdir(path.dirname(actualFilePath), { recursive: true }); - await fs.writeFile(actualFilePath, content); + await _writeFile(content, actualFilePath, { encoding: options.encoding }); return actualFilePath; }; +const _writeFile = async ( + content: string, + filePath: string, + options?: { encoding?: SupportedEncoding }, +): Promise => { + if (options && options.encoding) { + return fs.writeFile(filePath, iconv.encode(content, options.encoding)); + } + + return fs.writeFile(filePath, content); +}; + export const getRecordNumbers = (appId: string, apiToken: string): string[] => { const command = `record export --app ${appId} --base-url $$TEST_KINTONE_BASE_URL --api-token ${apiToken} --fields Record_number`; const response = execCliKintoneSync(command); diff --git a/features/utils/world.ts b/features/utils/world.ts index e88ca9f893..cc3f8a67ea 100644 --- a/features/utils/world.ts +++ b/features/utils/world.ts @@ -2,6 +2,7 @@ import type { SpawnSyncReturns } from "child_process"; import type { Credentials, AppCredential, Permission } from "./credentials"; import * as cucumber from "@cucumber/cucumber"; import { World } from "@cucumber/cucumber"; +import type { SupportedEncoding } from "./helper"; import { generateCsvFile, execCliKintoneSync, @@ -67,10 +68,18 @@ export class OurWorld extends World { }); } - public async generateCsvFile(inputCsvObject: string[][], filePath?: string) { - return generateCsvFile(inputCsvObject, { + public async generateCsvFile( + inputCsvObject: string[][], + options: { filePath?: string; encoding?: SupportedEncoding }, + ) { + const csvContent = inputCsvObject + .map((row) => row.map((field) => `"${field}"`).join(",")) + .join("\n"); + + return generateCsvFile(csvContent, { baseDir: this.workingDir, - destFilePath: filePath, + destFilePath: options?.filePath, + encoding: options?.encoding, }); } From a4c12e950af6f7fe61ac1889f65d004709349a1e Mon Sep 17 00:00:00 2001 From: hung-nguyen Date: Thu, 23 Nov 2023 17:12:30 +0700 Subject: [PATCH 2/4] fix: fix bug --- features/utils/world.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/utils/world.ts b/features/utils/world.ts index cc3f8a67ea..66322ae256 100644 --- a/features/utils/world.ts +++ b/features/utils/world.ts @@ -70,7 +70,7 @@ export class OurWorld extends World { public async generateCsvFile( inputCsvObject: string[][], - options: { filePath?: string; encoding?: SupportedEncoding }, + options?: { filePath?: string; encoding?: SupportedEncoding }, ) { const csvContent = inputCsvObject .map((row) => row.map((field) => `"${field}"`).join(",")) From e4408ff5d5f9c1e8e8da52eb12a7fad8f3257811 Mon Sep 17 00:00:00 2001 From: Nguyen Thai Hung <59815499+hung-cybo@users.noreply.github.com> Date: Fri, 24 Nov 2023 15:49:35 +0700 Subject: [PATCH 3/4] chore: update step definition Co-authored-by: tasshi / Masaharu TASHIRO <33759872+mshrtsr@users.noreply.github.com> --- features/step_definitions/import.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/step_definitions/import.ts b/features/step_definitions/import.ts index 969cbed5d6..77242858b6 100644 --- a/features/step_definitions/import.ts +++ b/features/step_definitions/import.ts @@ -12,7 +12,7 @@ Given( ); Given( - "The csv file {string} with {string} content as below:", + "The csv file {string} with {string} encoded content as below:", async function (filePath: string, encoding: SupportedEncoding, table) { if (!SUPPORTED_ENCODING.includes(encoding)) { throw new Error(`The encoding ${encoding} is not supported`); From 1ea6c1f7335de2fe5f52354c57f359b5ee6198ca Mon Sep 17 00:00:00 2001 From: hung-nguyen Date: Fri, 24 Nov 2023 16:07:26 +0700 Subject: [PATCH 4/4] fix: using new step definition --- features/import.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/import.feature b/features/import.feature index 9516d562fd..6f0c21a2ab 100644 --- a/features/import.feature +++ b/features/import.feature @@ -392,7 +392,7 @@ Feature: cli-kintone import command Scenario: CliKintoneTest-62 Should import the records successfully with --encoding option is utf8 Given The app "app_for_import" has no records - And The csv file "CliKintoneTest-62.csv" with "utf8" content as below: + And The csv file "CliKintoneTest-62.csv" with "utf8" encoded content as below: | Text | Number | | レコード番号 | 10 | And Load app ID of the app "app_for_import" as env var: "APP_ID" @@ -405,7 +405,7 @@ Feature: cli-kintone import command Scenario: CliKintoneTest-63 Should import the records successfully with --encoding option is sjis Given The app "app_for_import" has no records - And The csv file "CliKintoneTest-63.csv" with "sjis" content as below: + And The csv file "CliKintoneTest-63.csv" with "sjis" encoded content as below: | Text | Number | | 作成日時 | 10 | And Load app ID of the app "app_for_import" as env var: "APP_ID"