Skip to content

Commit

Permalink
[TS/JS] Move TS tests to dedicated folder and deps upgrade (#7508)
Browse files Browse the repository at this point in the history
* Move TS tests to dedicated folder and deps upgrade

* Attempt to fix generate_code

* Fix dir on CI

* Add js extension

* Fix missing extension

* Harmonize with test gen

* Unexplained code gen change

* Restore yarn.lock

* Naive attempt to fix bazel stuff

* Pin @bazel/typescript to 5.2.0

* Attempt to fix bazel

* More tweak

* Upgrade deps

* Tweak?

* Fix path

* Fix test package

Co-authored-by: Derek Bailey <derekbailey@google.com>
  • Loading branch information
bjornharrtell and dbaileychess committed Sep 13, 2022
1 parent 0360323 commit 4fca4dc
Show file tree
Hide file tree
Showing 125 changed files with 15,406 additions and 495 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Expand Up @@ -459,7 +459,7 @@ jobs:
- name: compile
run: yarn compile
- name: test
working-directory: tests
working-directory: tests/ts
run: python3 TypeScriptTest.py

build-dart:
Expand Down
4 changes: 2 additions & 2 deletions grpc/examples/ts/greeter/src/greeter_generated.ts
@@ -1,4 +1,4 @@
// automatically generated by the FlatBuffers compiler, do not modify

export { HelloReply } from './models/hello-reply';
export { HelloRequest } from './models/hello-request';
export { HelloReply } from './models/hello-reply.js';
export { HelloRequest } from './models/hello-request.js';
16 changes: 8 additions & 8 deletions package.json
Expand Up @@ -16,7 +16,7 @@
"test": "tests"
},
"scripts": {
"test": "npm run compile && cd tests && python3 ./TypeScriptTest.py",
"test": "npm run compile && cd tests/ts && python3 ./TypeScriptTest.py",
"compile": "tsc && tsc -p tsconfig.mjs.json && rollup -c",
"prepublishOnly": "npm install --only=dev && npm run compile"
},
Expand All @@ -35,12 +35,12 @@
"homepage": "https://google.github.io/flatbuffers/",
"dependencies": {},
"devDependencies": {
"@bazel/typescript": "^5.2.0",
"@types/node": "17.0.21",
"@typescript-eslint/eslint-plugin": "^4.12.0",
"@typescript-eslint/parser": "^4.12.0",
"eslint": "^7.17.0",
"rollup": "^2.78.0",
"typescript": "^4.5.5"
"@bazel/typescript": "5.2.0",
"@types/node": "18.7.16",
"@typescript-eslint/eslint-plugin": "^5.36.2",
"@typescript-eslint/parser": "^5.36.2",
"eslint": "^8.23.1",
"rollup": "^2.79.0",
"typescript": "^4.8.3"
}
}
25 changes: 21 additions & 4 deletions scripts/generate_code.py
Expand Up @@ -165,7 +165,6 @@ def glob(path, pattern):
NO_INCL_OPTS
+ CPP_OPTS
+ CS_OPTS
+ TS_OPTS
+ [
"--binary",
"--java",
Expand All @@ -180,6 +179,15 @@ def glob(path, pattern):
data="monsterdata_test.json",
)

flatc(
NO_INCL_OPTS
+ TS_OPTS,
schema="monster_test.fbs",
prefix="ts",
include="include_test",
data="monsterdata_test.json",
)

flatc(
["--lua", "--bfbs-filenames", str(tests_path)],
schema="monster_test.fbs",
Expand Down Expand Up @@ -235,14 +243,21 @@ def glob(path, pattern):
)

flatc(
BASE_OPTS + CPP_OPTS + CS_OPTS + TS_OPTS + JAVA_OPTS + KOTLIN_OPTS + PHP_OPTS,
BASE_OPTS + CPP_OPTS + CS_OPTS + JAVA_OPTS + KOTLIN_OPTS + PHP_OPTS,
prefix="union_vector",
schema="union_vector/union_vector.fbs",
)

flatc(
BASE_OPTS + TS_OPTS,
prefix="ts/union_vector",
schema="union_vector/union_vector.fbs",
)

flatc(
BASE_OPTS + TS_OPTS + ["--gen-name-strings", "--gen-mutable"],
include="include_test",
prefix="ts",
schema="monster_test.fbs",
)

Expand All @@ -257,13 +272,14 @@ def glob(path, pattern):
flatc(
BASE_OPTS + TS_OPTS + ["-b"],
include="include_test",
prefix="ts",
schema="monster_test.fbs",
data="unicode_test.json",
)

flatc(
BASE_OPTS + TS_OPTS + ["--gen-name-strings"],
prefix="union_vector",
prefix="ts/union_vector",
schema="union_vector/union_vector.fbs",
)

Expand Down Expand Up @@ -357,7 +373,8 @@ def glob(path, pattern):

# Optional Scalars
optional_scalars_schema = "optional_scalars.fbs"
flatc(["--java", "--kotlin", "--lobster", "--ts"], schema=optional_scalars_schema)
flatc(["--java", "--kotlin", "--lobster"], schema=optional_scalars_schema)
flatc(TS_OPTS, schema=optional_scalars_schema, prefix="ts")

flatc(["--csharp", "--python", "--gen-object-api"], schema=optional_scalars_schema)

Expand Down
6 changes: 3 additions & 3 deletions src/idl_gen_ts.cpp
Expand Up @@ -263,7 +263,7 @@ class TsGenerator : public BaseGenerator {
namer_.EscapeKeyword(pair.second) + ", ";
}
code.resize(code.size() - 2);
code += "} from '" + include_name + "';\n";
code += "} from '" + include_name + ".js';\n";
}
code += "\n";
}
Expand Down Expand Up @@ -760,9 +760,9 @@ class TsGenerator : public BaseGenerator {
import.bare_file_path = bare_file_path;
import.rel_file_path = rel_file_path;
import.import_statement =
"import { " + symbols_expression + " } from '" + rel_file_path + "';";
"import { " + symbols_expression + " } from '" + rel_file_path + ".js';";
import.export_statement =
"export { " + symbols_expression + " } from '." + bare_file_path + "';";
"export { " + symbols_expression + " } from '." + bare_file_path + ".js';";
import.dependency = &dependency;
import.dependent = &dependent;

Expand Down
10 changes: 0 additions & 10 deletions tests/BUILD.bazel
@@ -1,6 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_test")
load("//:build_defs.bzl", "flatbuffer_cc_library")
load("//:typescript.bzl", "flatbuffer_ts_library")

package(default_visibility = ["//visibility:private"])

Expand Down Expand Up @@ -215,12 +214,3 @@ flatbuffer_cc_library(
"--cpp-ptr-type flatbuffers::unique_ptr",
],
)

flatbuffer_ts_library(
name = "typescript_ts_fbs",
srcs = ["typescript_keywords.fbs"],
deps = [
"//tests/test_dir:include_ts_fbs",
"//tests/test_dir:typescript_transitive_ts_fbs",
],
)
32 changes: 16 additions & 16 deletions tests/flatc/flatc_ts_tests.py
Expand Up @@ -26,8 +26,8 @@ def Base(self):
assert_file_and_contents(
"foo_generated.ts",
[
"export { Bar } from './bar';",
"export { Foo } from './foo';",
"export { Bar } from './bar.js';",
"export { Foo } from './foo.js';",
],
)

Expand All @@ -46,8 +46,8 @@ def BaseMultipleFiles(self):
assert_file_and_contents(
"foo_generated.ts",
[
"export { Bar } from './bar';",
"export { Foo } from './foo';",
"export { Bar } from './bar.js';",
"export { Foo } from './foo.js';",
],
)

Expand All @@ -66,8 +66,8 @@ def BaseWithNamespace(self):
assert_file_and_contents(
"foo_with_ns_generated.ts",
[
"export { Bar } from './bar/bar';",
"export { Foo } from './something/foo';",
"export { Bar } from './bar/bar.js';",
"export { Foo } from './something/foo.js';",
],
)

Expand All @@ -77,7 +77,7 @@ def BaseWithNamespace(self):
"something/foo.ts",
[
"export class Foo {",
"import { Bar } from '../bar/bar';",
"import { Bar } from '../bar/bar.js';",
],
)

Expand All @@ -92,17 +92,17 @@ def GenAll(self):
assert_file_and_contents(
"foo_generated.ts",
[
"export { Bar } from './bar'",
"export { Baz } from './baz'",
"export { Foo } from './foo'",
"export { Bar } from './bar.js'",
"export { Baz } from './baz.js'",
"export { Foo } from './foo.js'",
],
)

# Foo should be generated with an import to Bar and an export of itself.
assert_file_and_contents(
"foo.ts",
[
"import { Bar } from './bar';",
"import { Bar } from './bar.js';",
"export class Foo {",
],
)
Expand All @@ -111,7 +111,7 @@ def GenAll(self):
assert_file_and_contents(
"bar.ts",
[
"import { Baz } from './baz';",
"import { Baz } from './baz.js';",
"export class Bar {",
],
)
Expand All @@ -134,7 +134,7 @@ def FlatFiles(self):
assert_file_and_contents(
"foo_generated.ts",
[
"import {Bar as Bar} from './bar_generated';",
"import {Bar as Bar} from './bar_generated.js';",
"export class Foo {",
],
)
Expand All @@ -151,7 +151,7 @@ def FlatFilesWithNamespace(self):
assert_file_and_contents(
"foo_with_ns_generated.ts",
[
"import {Bar as Bar} from './bar_with_ns_generated';",
"import {Bar as Bar} from './bar_with_ns_generated.js';",
"export class Foo {",
],
)
Expand All @@ -168,7 +168,7 @@ def FlatFilesMultipleFiles(self):
assert_file_and_contents(
"foo_generated.ts",
[
"import {Bar as Bar} from './bar_generated';",
"import {Bar as Bar} from './bar_generated.js';",
"export class Foo {",
],
)
Expand All @@ -178,7 +178,7 @@ def FlatFilesMultipleFiles(self):
assert_file_and_contents(
"bar_generated.ts",
[
"import {Baz as Baz} from './baz_generated';",
"import {Baz as Baz} from './baz_generated.js';",
"export class Bar {",
],
)
Expand Down
7 changes: 0 additions & 7 deletions tests/package.json

This file was deleted.

12 changes: 12 additions & 0 deletions tests/ts/BUILD.bazel
@@ -0,0 +1,12 @@
load("//:typescript.bzl", "flatbuffer_ts_library")

package(default_visibility = ["//visibility:private"])

flatbuffer_ts_library(
name = "typescript_ts_fbs",
srcs = ["typescript_keywords.fbs"],
deps = [
"//tests/ts/test_dir:include_ts_fbs",
"//tests/ts/test_dir:typescript_transitive_ts_fbs",
],
)
@@ -1,7 +1,7 @@
// Run this using JavaScriptTest.sh
import assert from 'assert'
import fs from 'fs'
import * as flexbuffers from 'flatbuffers/js/flexbuffers'
import * as flexbuffers from 'flatbuffers/js/flexbuffers.js'

function main() {
testSingleValueBuffers();
Expand Down Expand Up @@ -341,7 +341,7 @@ function testRoundTripWithBuilder() {
}

function testGoldBuffer() {
const data = new Uint8Array(fs.readFileSync('gold_flexbuffer_example.bin')).buffer;
const data = new Uint8Array(fs.readFileSync('../gold_flexbuffer_example.bin')).buffer;
const b1 = flexbuffers.toReference(data).get("bools").get(1);
assert.strictEqual(b1.isBool(), true);
assert.strictEqual(b1.boolValue(), false);
Expand Down
18 changes: 9 additions & 9 deletions tests/JavaScriptTest.js → tests/ts/JavaScriptTest.js
Expand Up @@ -3,18 +3,18 @@ import assert from 'assert'
import fs from 'fs'
import * as flatbuffers from 'flatbuffers'

import { Monster, MonsterT } from './my-game/example/monster'
import { Test, TestT } from './my-game/example/test'
import { Stat } from './my-game/example/stat'
import { Vec3 } from './my-game/example/vec3'
import { Color } from './my-game/example/color';
import { Any } from './my-game/example/any';
import { Monster, MonsterT } from './my-game/example/monster.js'
import { Test, TestT } from './my-game/example/test.js'
import { Stat } from './my-game/example/stat.js'
import { Vec3 } from './my-game/example/vec3.js'
import { Color } from './my-game/example/color.js';
import { Any } from './my-game/example/any.js';

function main() {

// First, let's test reading a FlatBuffer generated by C++ code:
// This file was generated from monsterdata_test.json
var data = new Uint8Array(fs.readFileSync('monsterdata_test.mon'));
var data = new Uint8Array(fs.readFileSync('../monsterdata_test.mon'));

// Now test it:

Expand Down Expand Up @@ -285,8 +285,8 @@ function test64bit() {
}

function testUnicode() {
var correct = fs.readFileSync('unicode_test.mon');
var json = JSON.parse(fs.readFileSync('unicode_test.json', 'utf8'));
var correct = fs.readFileSync('../unicode_test.mon');
var json = JSON.parse(fs.readFileSync('../unicode_test.json', 'utf8'));

// Test reading
function testReadingUnicode(bb) {
Expand Down
@@ -1,10 +1,10 @@
import assert from 'assert'
import * as flatbuffers from 'flatbuffers'

import { Character } from './union_vector/character'
import { BookReader, BookReaderT } from './union_vector/book-reader'
import { Attacker, AttackerT } from './union_vector/attacker'
import { Movie, MovieT } from './union_vector/movie'
import { Character } from './union_vector/character.js'
import { BookReader, BookReaderT } from './union_vector/book-reader.js'
import { Attacker, AttackerT } from './union_vector/attacker.js'
import { Movie, MovieT } from './union_vector/movie.js'

var charTypes = [
Character.Belle,
Expand Down

0 comments on commit 4fca4dc

Please sign in to comment.