Skip to content

Commit

Permalink
[terra-core/terra] Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
littleGnAl committed Sep 19, 2023
1 parent 3b55dc0 commit 5abfacf
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 13 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Unit test & integration test

on:
pull_request:

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "16.x"

- name: Run unit test
run: bash tool/run_ut.sh
107 changes: 107 additions & 0 deletions terra-core/__tests__/unit_test/path_resolver.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
});


6 changes: 6 additions & 0 deletions terra-core/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['node_modules/', '.dist/'],
testRegex: '.*\\.test\\.ts$',
};
7 changes: 6 additions & 1 deletion terra-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 8 additions & 7 deletions terra-core/src/path_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import fs from "fs";

import path from "path";

import os from 'os';

/**
* Resolve the schema: `<module_name>:<path>`, or absolute path, or relative path
*/
Expand All @@ -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
Expand All @@ -30,22 +32,21 @@ 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));

let parserPackageDir = path.join(currentPackageNodeModuleDir, module);
console.assert(fs.existsSync(parserPackageDir));

return parserPackageDir;
return path.resolve(parserPackageDir);
}

export function requireModule(module: string): any {
Expand All @@ -67,5 +68,5 @@ export function requireModule(module: string): any {
parserPackageJsonIndexFilePath
);

return require(parserPackageIndexFilePath);
return require(path.resolve(parserPackageIndexFilePath));
}
6 changes: 3 additions & 3 deletions terra-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion terra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"typescript": "^4.8.4",
"commander": "^9.4.1",
"mustache": "^4.2.0",
"yaml": "^2.1.3"
"yaml": "^2.1.3",
"@agoraio-extensions/terra-core": ">=0.1.0"
},
"description": ""
}
14 changes: 14 additions & 0 deletions tool/run_ut.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5abfacf

Please sign in to comment.