diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..191d028 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,25 @@ +name: Unit test & integration test + +on: + pull_request: + +jobs: + publish: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v3 + # Setup .npmrc file to publish to GitHub Packages + - uses: actions/setup-node@v3 + with: + node-version: "16.x" + registry-url: "https://npm.pkg.github.com" + # Defaults to the user or organization that owns the workflow file + scope: "@agoraio-extensions" + + - name: Run unit test + env: + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: bash tool/run_ut.sh diff --git a/terra-core/__tests__/unit_test/path_resolver.test.ts b/terra-core/__tests__/unit_test/path_resolver.test.ts new file mode 100644 index 0000000..5d14f6a --- /dev/null +++ b/terra-core/__tests__/unit_test/path_resolver.test.ts @@ -0,0 +1,107 @@ +import fs from "fs"; + +import path from "path"; + +import os from 'os'; + +import { requireModule, resolveModulePath, resolvePath } from "../../src/path_resolver"; + + + +describe("path_resolver", () => { + const originalEnv = process.env; + + let tmpDir: string = ""; + let tmpPackageJsonPath: string = ""; + let tmpNodeModulesDir: string = ""; + + beforeEach(() => { + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'terra-ut-')); + tmpPackageJsonPath = path.join(tmpDir, "package.json"); + + tmpNodeModulesDir = path.join(tmpDir, "node_modules"); + + fs.mkdirSync(tmpNodeModulesDir); + + jest.resetModules(); + process.env = { + ...originalEnv, + npm_package_json: tmpPackageJsonPath, + }; + }); + + afterEach(() => { + fs.rmSync(tmpDir, { recursive: true, force: true }); + process.env = originalEnv; + }); + + describe("resolvePath", () => { + it("can resolve absolute path", () => { + expect(resolvePath("/a/b/c")).toEqual("/a/b/c"); + }); + + it("can resolve absolute path with prefix", () => { + expect(resolvePath("/a/b/c", "/d/e/f")).toEqual("/a/b/c"); + }); + + it("can resolve relative path", () => { + expect(resolvePath("a/b/c")).toEqual(path.resolve("a/b/c")); + }); + + it("can resolve relative path with prefix", () => { + expect(resolvePath("a/b/c", "/d/e/f")).toEqual("/d/e/f/a/b/c"); + }); + + it("can resolve path with package schema", () => { + fs.mkdirSync(path.join(tmpNodeModulesDir, "aaa")); + + let res = resolvePath("aaa:index.ts"); + + let expectRes = path.join(tmpNodeModulesDir, "aaa", "index.ts"); + + expect(res).toEqual(expectRes); + }); + }); + + describe("resolveModulePath", () => { + it("can resolve module path", () => { + fs.mkdirSync(path.join(tmpNodeModulesDir, "aaa")); + + let res = resolveModulePath("aaa"); + + let expectRes = path.join(tmpNodeModulesDir, "aaa"); + + expect(res).toEqual(expectRes); + }); + }); + + describe("requireModule", () => { + it("can require module", () => { + let testPackageDir = path.join(tmpNodeModulesDir, "aaa"); + fs.mkdirSync(testPackageDir); + + let testPackageJsonPath = path.join(testPackageDir, "package.json"); + let testPackageJsonContent = ` +{ + "version": "0.1.2", + "main": "index.js" +} +`; + fs.writeFileSync(testPackageJsonPath, testPackageJsonContent); + + let testPackageIndexJsPath = path.join(testPackageDir, "index.js"); + let testPackageIndexJsContent = ` +module.exports = "aaa"; +`; + + fs.writeFileSync(testPackageIndexJsPath, testPackageIndexJsContent); + + + let res = requireModule("aaa"); + + expect(res).toEqual("aaa"); + }); + }); +}); + + diff --git a/terra-core/jest.config.js b/terra-core/jest.config.js new file mode 100644 index 0000000..99c689c --- /dev/null +++ b/terra-core/jest.config.js @@ -0,0 +1,6 @@ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', + testPathIgnorePatterns: ['node_modules/', '.dist/'], + testRegex: '.*\\.test\\.ts$', +}; diff --git a/terra-core/package.json b/terra-core/package.json index 7189f91..6210aa4 100644 --- a/terra-core/package.json +++ b/terra-core/package.json @@ -7,10 +7,15 @@ "version": "0.1.2", "main": "src/index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "jest" }, "author": "", "license": "ISC", + "devDependencies": { + "@types/jest": "^29.5.1", + "jest": "^29.5.0", + "ts-jest": "^29.1.0" + }, "dependencies": { "@types/node": "^20.5.9", "ts-node": "^10.9.1", diff --git a/terra-core/src/path_resolver.ts b/terra-core/src/path_resolver.ts index aba3907..d5aa98d 100644 --- a/terra-core/src/path_resolver.ts +++ b/terra-core/src/path_resolver.ts @@ -2,6 +2,8 @@ import fs from "fs"; import path from "path"; +import os from 'os'; + /** * Resolve the schema: `:`, or absolute path, or relative path */ @@ -15,10 +17,10 @@ export function resolvePath( } if (prefixDirIfNotAbsolute == "") { - return p; + return path.resolve(p); } - return path.join(prefixDirIfNotAbsolute, p); + return path.resolve(path.join(prefixDirIfNotAbsolute, p)); } // e.g., shared_configs:headers/rtc_4.2.3/shared_configs.yaml @@ -30,14 +32,13 @@ export function resolvePath( let localModulePath = resolveModulePath(moduleName); let localPathInModule = path.join(localModulePath, pathInModule); - return localPathInModule; + return path.resolve(localPathInModule); } export function resolveModulePath(module: string): string { let currentPackageJsonPath = process.env.npm_package_json; let currentPackageNodeModuleDir = path.join( - currentPackageJsonPath as string, - "..", + path.dirname(currentPackageJsonPath as string), "node_modules" ); console.assert(fs.existsSync(currentPackageNodeModuleDir)); @@ -45,7 +46,7 @@ export function resolveModulePath(module: string): string { let parserPackageDir = path.join(currentPackageNodeModuleDir, module); console.assert(fs.existsSync(parserPackageDir)); - return parserPackageDir; + return path.resolve(parserPackageDir); } export function requireModule(module: string): any { @@ -67,5 +68,5 @@ export function requireModule(module: string): any { parserPackageJsonIndexFilePath ); - return require(parserPackageIndexFilePath); + return require(path.resolve(parserPackageIndexFilePath)); } diff --git a/terra-core/tsconfig.json b/terra-core/tsconfig.json index a883dc9..9334641 100644 --- a/terra-core/tsconfig.json +++ b/terra-core/tsconfig.json @@ -26,12 +26,12 @@ /* Modules */ "module": "commonjs", /* Specify what module code is generated. */ - "rootDir": "./src", /* Specify the root folder within your source files. */ + // "rootDir": "./src", /* Specify the root folder within your source files. */ // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ - // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ - // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ + "rootDirs": ["./src", "__tests__/unit_test"], /* Allow multiple folders to be treated as one when resolving modules. */ + "typeRoots": ["./node_modules/@types"], /* Specify multiple folders that act like './node_modules/@types'. */ // "types": [], /* Specify type package names to be included without being referenced in a source file. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ diff --git a/terra/__tests__/unit_test/renderers/dump_json_renderer.test.ts b/terra/__tests__/unit_test/renderers/dump_json_renderer.test.ts index 83bda91..d986d44 100644 --- a/terra/__tests__/unit_test/renderers/dump_json_renderer.test.ts +++ b/terra/__tests__/unit_test/renderers/dump_json_renderer.test.ts @@ -4,7 +4,10 @@ import path from 'path'; import { ParseResult, TerraContext } from '@agoraio-extensions/terra-core'; -import { dumpJsonRenderer, terraAstJsonFileName } from './dump_json_renderer'; +import { + dumpJsonRenderer, + terraAstJsonFileName, +} from '../../../src/renderers/dump_json_renderer'; interface FakeNode { name: string; diff --git a/tool/run_ut.sh b/tool/run_ut.sh new file mode 100644 index 0000000..8eb592a --- /dev/null +++ b/tool/run_ut.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -e +set -x + +# Find all the `__tests__`` directories to ensure there're test cases there +for d in **/*__tests__*; do + PACKAGE_DIR=$(realpath ${d}/..) + echo "Running tests in ${PACKAGE_DIR}"; + pushd $PACKAGE_DIR + npm install + npm run test + popd +done +