Skip to content

Commit

Permalink
fix path issues on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffhendrey committed May 20, 2024
1 parent 960d91a commit 1028769
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 62 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions src/CliCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import VizGraph from "./VizGraph.js";
import { exec } from 'child_process';
import http from 'http';
import * as child_process from "child_process";
import os from "os";


export default class CliCore {
Expand Down Expand Up @@ -115,14 +116,14 @@ export default class CliCore {
if (filepath && filepath.startsWith("/")) throw new Error("Cannot use file path starting with '/' with importPath");
if (importPath.startsWith("/")) return path.resolve(path.join(importPath, filepath))

if (importPath.startsWith("~")) return path.resolve(path.join(importPath.replace("~", process.env.HOME), filepath));
if (importPath.startsWith("~")) return path.resolve(path.join(importPath.replace("~", os.homedir()), filepath));

//relative path
return path.resolve(path.join(process.cwd(), importPath, filepath));
return path.resolve(path.join(importPath, filepath));
}

if (filepath && filepath.includes("~")) return path.resolve(filepath.replace("~", process.env.HOME));
if (filepath && filepath.startsWith("/")) return filepath;
if (filepath && filepath.includes("~")) return path.normalize(path.resolve(filepath.replace("~", os.homedir())));
if (filepath && filepath.startsWith("/")) return path.normalize(filepath);
return path.join(process.cwd(), filepath);
}

Expand Down Expand Up @@ -579,5 +580,14 @@ export default class CliCore {
return `http://localhost:${port}`;
}
}

/**
* this method is just here as a stub to allow tests to pass. Color is in reality handled only by StatedRepl,
* it is not something that is possible to 'see' from the CLI since CLI returns pure JSON which has no concept
* of terminal colors.
*/
color(){
"";
}
}

13 changes: 12 additions & 1 deletion src/StatedREPL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class StatedREPL {

async initialize() {
this.isColorized = false;
const cmdLineArgsStr = process.argv.slice(2).join(" ");
const cmdLineArgsStr = StatedREPL.getCmdLineArgsStr();
const {oneshot} = CliCore.parseInitArgs(cmdLineArgsStr)
const resp = await this.cliCore.init(cmdLineArgsStr)
if(oneshot){
Expand All @@ -69,6 +69,17 @@ export default class StatedREPL {
this.registerCommands();
}

private static getCmdLineArgsStr() {
return process.argv.slice(2).map((s) => {
if (s.includes(" ")) {
if (!s.startsWith('"')) {
return '"' + s + '"';//we need to surround any arguments that have whitespace like "...Program Files ..." with quotes sp we don't break our own argument parsing
}
}
return s;
}).join(" ");
}

close(){
this.cliCore.close();
this.r.close();
Expand Down
45 changes: 35 additions & 10 deletions src/test/CliCore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,41 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import path from "path";
import os from "os";
import CliCore from "../../dist/src/CliCore.js";

const homeDir = os.homedir();

const testCases = [
{
filePath: "file/Path",
importPath: undefined,
expected: `${process.cwd()}/file/Path`,
expected: path.resolve(process.cwd(), "file/Path"),
description: "should resolve a relative filePath"
},
{
filePath: "file/Path",
importPath: "import//Path",
expected: `${process.cwd()}/import/Path/file/Path`,
importPath: "import/Path",
expected: path.resolve(process.cwd(), "import/Path", "file/Path"),
description: "should resolve a relative import path with both filePath and importPath"
},
{
filePath: "file/Path",
importPath: "/import/Path",
expected: "/import/Path/file/Path",
expected: path.resolve("/", "import/Path", "file/Path"),
description: "should resolve an absolute import path"
},
{
filePath: "file/Path",
importPath: "~/import/Path",
expected: `${process.env.HOME}/import/Path/file/Path`,
expected: path.resolve(homeDir, "import/Path", "file/Path"),
description: "should resolve a tilde in importPath with filePath"
},
{
filePath: "~/file/Path",
importPath: undefined,
expected: `${process.env.HOME}/file/Path`,
expected: path.resolve(homeDir, "file/Path"),
description: "should resolve a tilde in filePath"
},
{
Expand All @@ -53,9 +57,9 @@ const testCases = [
}
];

testCases.forEach(({filePath, importPath, expected, description}) => {
testCases.forEach(({ filePath, importPath, expected, description }) => {
test(description, async () => {
expect(CliCore.resolveImportPath(filePath, importPath)).toBe(expected);
expect(path.normalize(CliCore.resolveImportPath(filePath, importPath))).toBe(path.normalize(expected));
});
});

Expand All @@ -80,7 +84,7 @@ const errorTestCases = [
}
];

errorTestCases.forEach(({filePath, importPath, error, description}) => {
errorTestCases.forEach(({ filePath, importPath, error, description }) => {
test(description, async () => {
expect(() => CliCore.resolveImportPath(filePath, importPath)).toThrowError(error);
});
Expand All @@ -106,7 +110,28 @@ test("tail", async () => {
expect(res).toStrictEqual({
"__tailed": true,
"data": 5
})
});
cliCore.close();
});

test("xf", async () => {
const cliCore = new CliCore();
const res = await cliCore.init('.init -f "example/useEnv.json" --xf=example/env.json');
expect(res).toStrictEqual({
"name": "Dr. Stephen Falken",
"address": "Goose Island, OR, USA"
});
cliCore.close();
});

test("import JS", async () => {
const cliCore = new CliCore();
const res = await cliCore.init('.init -f example/importJS.json --xf=example/test-export.js');
expect(res).toStrictEqual({
"res": "bar: foo"
});
cliCore.close();
});



2 changes: 1 addition & 1 deletion src/test/TemplateProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2546,7 +2546,7 @@ test("performance test with 100 data injections", async () => {
await tp.setData("/data", newData);
}
let endTime = Date.now();
console.log(`Total execution time for 100 data sets: ${endTime - startTime} ms`);
console.log(`Total execution time for 10000 data sets: ${endTime - startTime} ms`);
console.log(`rules per second: ${10000/((endTime - startTime)/1000)}`);

});
Expand Down
Loading

0 comments on commit 1028769

Please sign in to comment.