From 4fc2e3938fb96ac62e62b4eccf7615bf5168cc55 Mon Sep 17 00:00:00 2001 From: "Steven R. Loomis" Date: Thu, 16 May 2024 12:09:40 -0500 Subject: [PATCH] chore(core): Unicode version test - support wasm: copy package.json, nodeversions.json and Blocks.txt into the keyboard area so that they can be mounted under wasm - path changes to make the non-wasm part work as well Fixes: #10183 --- core/tests/unit/ldml/meson.build | 43 +++++++---- core/tests/unit/ldml/test_unicode.cpp | 80 ++++++++++++++------- core/tests/unit/ldml/write_node_versions.js | 8 +++ 3 files changed, 94 insertions(+), 37 deletions(-) create mode 100644 core/tests/unit/ldml/write_node_versions.js diff --git a/core/tests/unit/ldml/meson.build b/core/tests/unit/ldml/meson.build index 72aa107e7f8..7b3705fcd22 100644 --- a/core/tests/unit/ldml/meson.build +++ b/core/tests/unit/ldml/meson.build @@ -37,29 +37,42 @@ endif # Build ldml test executable +keyboard_build_path = join_paths(meson.current_build_dir(),'keyboards') + if cpp_compiler.get_id() == 'emscripten' tests_flags = ['--embed-file', join_paths(meson.current_build_dir(),'keyboards','@')] tests_flags += ['--embed-file', join_paths(meson.current_build_dir(),'invalid-keyboards','@')] + tests_flags += ['--embed-file', join_paths(meson.current_build_dir(),'nodeversions.json') + '@nodeversions.json'] + tests_flags += ['--embed-file', join_paths(meson.current_build_dir(),'package.json') + '@package.json'] + tests_flags += ['--embed-file', join_paths(meson.current_build_dir(),'Blocks.txt') + '@Blocks.txt'] test_path = '/' + test_unicode_path = '/' invalid_test_path = '/' + tests_flags += ['-lnodefs.js', + '-sNO_DISABLE_EXCEPTION_CATCHING', # for test exceptions + '-sEXPORTED_RUNTIME_METHODS=[\'UTF8ToString\']'] else tests_flags = [] test_path = join_paths(meson.current_build_dir(),'keyboards') + test_unicode_path = join_paths(meson.current_build_dir()) invalid_test_path = join_paths(meson.current_build_dir(),'invalid-keyboards') endif -if cpp_compiler.get_id() == 'emscripten' - # TODO: we will move the tests to ES6 in the future - # '-sMODULARIZE', '-sEXPORT_ES6', - tests_flags += ['-lnodefs.js', '-sEXPORTED_RUNTIME_METHODS=[\'UTF8ToString\']'] -endif +# copy package.json into build dir for test use +configure_file( + copy: true, + input: '../../../../package.json', + output: 'package.json', +) +configure_file( + copy: true, + input: '../../../../resources/standards-data/unicode-character-database/Blocks.txt', + output: 'Blocks.txt', +) -# collect some data from node.js -node_versions = custom_target( - 'nodeversions.json', - command: [node, '-p', 'JSON.stringify(process.versions)'], - capture: true, # collect stdout from nodejs - output: ['nodeversions.json'], +configure_file( + command: [node, join_paths(meson.current_source_dir(), 'write_node_versions.js'),'@OUTPUT@'], + output: 'nodeversions.json', ) ldml = executable('ldml', @@ -124,8 +137,14 @@ u = executable('test_unicode', 'test_unicode.cpp', objects: lib.extract_all_objects(recursive: false), ) -test('test_unicode', u, suite: 'ldml', args: [node_versions]) +test('test_unicode', u, suite: 'ldml', + args: [ + join_paths(test_unicode_path, 'nodeversions.json'), + join_paths(test_unicode_path, 'package.json'), + join_paths(test_unicode_path, 'Blocks.txt'), + ], +) # Run tests on all keyboards (`tests` defined in keyboards/meson.build) diff --git a/core/tests/unit/ldml/test_unicode.cpp b/core/tests/unit/ldml/test_unicode.cpp index f3e42fae3b0..77ae0202aca 100644 --- a/core/tests/unit/ldml/test_unicode.cpp +++ b/core/tests/unit/ldml/test_unicode.cpp @@ -50,7 +50,8 @@ nlohmann::json load_json(const km::core::path &jsonpath) { std::cout << "== " << __FUNCTION__ << " loading " << jsonpath << std::endl; std::ifstream json_file(jsonpath.native()); if (!json_file) { - return -1; // no file + std::cerr << "ERROR Could not load: " << jsonpath << std::endl; + assert (json_file); } nlohmann::json data = nlohmann::json::parse(json_file); return data; @@ -70,10 +71,12 @@ std::string get_major(const std::string& ver) { return ver.substr(start, end - start); } -std::string get_block_unicode_ver() { +/** @return the Unicode version from a Blocks.txt file */ +std::string get_block_unicode_ver(const char *blocks_path) { + std::cout << "= " << __FUNCTION__ << " load " << blocks_path << std::endl; // fetch Blocks.txt std::ifstream blocks_file( - km::core::path("../../../../resources/standards-data/unicode-character-database/Blocks.txt").native()); + km::core::path(blocks_path).native()); assert(blocks_file.good()); std::string block_line; assert(std::getline(blocks_file, block_line)); // first line @@ -82,16 +85,37 @@ std::string get_block_unicode_ver() { return block_line.substr(prefix.length()); } -void test_unicode_versions(const nlohmann::json &versions, const nlohmann::json &package) { - std::cout << "== " << __FUNCTION__ << std::endl; +void test_unicode_versions(const nlohmann::json &versions, const nlohmann::json &package, +const std::string &block_unicode_ver) { + std::cout << "== test: " << __FUNCTION__ << std::endl; - const std::string cxx_icu(U_ICU_VERSION), - cxx_icu_unicode(U_UNICODE_VERSION), - node_icu_unicode(versions["/unicode"_json_pointer].template get()), - node(versions["/node"_json_pointer].template get()), - node_icu(versions["/icu"_json_pointer].template get()), - node_engine(package["/engines/node"_json_pointer].template get()); - const auto block_unicode_ver = get_block_unicode_ver(); +#define SHOW_VAR(x) (std::cout << #x << "\t" << (x) << std::endl) + + const std::string cxx_icu(U_ICU_VERSION); + SHOW_VAR(cxx_icu); + + const std::string cxx_icu_unicode(U_UNICODE_VERSION); + SHOW_VAR(cxx_icu_unicode); + + SHOW_VAR(versions); + + const std::string node_icu_unicode(versions["unicode"].template get()); + SHOW_VAR(node_icu_unicode); + SHOW_VAR(versions["node"]); + + const std::string node(versions["node"].template get()); + SHOW_VAR(node); + + const std::string node_icu(versions["icu"].template get()); + SHOW_VAR(node_icu); + + const std::string node_engine(package["engines"]["node"].template get()); + SHOW_VAR(node_engine); + + std::cout << "=== Loaded from JSON" << std::endl; + + SHOW_VAR(block_unicode_ver); + std::cout << "=== calculating major versions" << std::endl; // calculations auto block_ver_major = get_major(block_unicode_ver); @@ -101,14 +125,7 @@ void test_unicode_versions(const nlohmann::json &versions, const nlohmann::json auto node_icu_major = get_major(node_icu); auto cxx_icu_unicode_major = get_major(cxx_icu_unicode); auto node_icu_unicode_major = get_major(node_icu_unicode); -#define SHOW_VAR(x) (std::cout << #x << "\t" << (x) << std::endl) - SHOW_VAR(cxx_icu); - SHOW_VAR(cxx_icu_unicode); - SHOW_VAR(node_icu_unicode); - SHOW_VAR(node); - SHOW_VAR(node_icu); - SHOW_VAR(node_engine); - SHOW_VAR(block_unicode_ver); + #undef SHOW_VAR // allow the Node.js version to be >= required @@ -129,7 +146,7 @@ void test_unicode_versions(const nlohmann::json &versions, const nlohmann::json std::cout << std::endl; } -int test_all(const char *jsonpath) { +int test_all(const char *jsonpath, const char *packagepath, const char *blockspath) { std::cout << "= " << __FUNCTION__ << std::endl; // load the dump of node's process.versions which the meson.build file generated @@ -137,9 +154,12 @@ int test_all(const char *jsonpath) { assert(!versions.empty()); // load our top level package.json - auto package = load_json(km::core::path("../../../../package.json")); + auto package = load_json(km::core::path(packagepath)); + assert(!package.empty()); + + const auto block_unicode_ver = get_block_unicode_ver(blockspath); - test_unicode_versions(versions, package); + test_unicode_versions(versions, package, block_unicode_ver); return EXIT_SUCCESS; } @@ -149,7 +169,7 @@ int test_all(const char *jsonpath) { //------------------------------------------------------------------------------------- constexpr const auto help_str = "\ -test_unicode [--color] nodeversions.json\n\ +test_unicode [--color] nodeversions.json package.json Blocks.txt\n\ \n\ --color Force color output\n"; @@ -183,7 +203,17 @@ int main(int argc, char *argv []) { } auto jsonpath = argv[first_arg++]; - int rc = test_all(jsonpath); + if (argc <= first_arg) { + return error_args(); + } + auto packagepath = argv[first_arg++]; + + if (argc <= first_arg) { + return error_args(); + } + auto blockspath = argv[first_arg++]; + + int rc = test_all(jsonpath, packagepath, blockspath); return rc; } diff --git a/core/tests/unit/ldml/write_node_versions.js b/core/tests/unit/ldml/write_node_versions.js new file mode 100644 index 00000000000..ae8cd880b32 --- /dev/null +++ b/core/tests/unit/ldml/write_node_versions.js @@ -0,0 +1,8 @@ +// Simple utility to dump process.versions to argument 1 + +const { argv, versions } = require('process'); +const { writeFileSync } = require('fs'); + +const [ f ] = argv.slice(2); +writeFileSync(f, JSON.stringify(versions, null, ' ')); +console.log('Wrote:', f);