Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[terra-core/terra] Add unit test #13

Merged
merged 5 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -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
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
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