Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
38cde77
added new deno settings
yunchengyang515 Nov 11, 2023
a18f3be
added new deno settings
yunchengyang515 Nov 11, 2023
544c7c6
Merge pull request #2 from stackql/dev/deno-vscode-setup
yunchengyang515 Nov 11, 2023
b05fe45
Created downloader and tests
yunchengyang515 Nov 12, 2023
26eede0
able to run query
yunchengyang515 Nov 12, 2023
55a1cdf
fix tests
yunchengyang515 Nov 12, 2023
b4799ca
attempt to fix file close
yunchengyang515 Nov 12, 2023
9186f8d
added multiple OS tests
yunchengyang515 Nov 12, 2023
2b5674b
WIP: fixing test issues
yunchengyang515 Nov 13, 2023
4a953cc
fix the Deno.run issue in unpacker
yunchengyang515 Nov 13, 2023
76a9569
replace deno.run with deno.command
yunchengyang515 Nov 13, 2023
db40abf
added allow execute for the binary
yunchengyang515 Nov 13, 2023
3fb7b6d
added more log to check windows OS issue
yunchengyang515 Nov 13, 2023
9063ab0
added more logging
yunchengyang515 Nov 13, 2023
4f8a442
fix the chomod issue
yunchengyang515 Nov 13, 2023
921aa5a
code clean up
yunchengyang515 Nov 13, 2023
d1ab0ae
Merge pull request #4 from stackql/dev/setup-github-test-action
yunchengyang515 Nov 13, 2023
29c1bf9
Merge pull request #3 from stackql/dev/install-stackql
jeffreyaven Nov 16, 2023
9d35ae3
added server connection and test
yunchengyang515 Nov 18, 2023
c529e14
added setup stackql in github action
yunchengyang515 Nov 18, 2023
77b2540
fix the process closing for server setup
yunchengyang515 Nov 18, 2023
b902d94
test only windows
yunchengyang515 Nov 18, 2023
c304703
added back linux and macos
yunchengyang515 Nov 18, 2023
526d095
add timeout to wait for stackql to start
yunchengyang515 Nov 18, 2023
19b0913
added retries
yunchengyang515 Nov 18, 2023
f6303df
remove timeout in start server
yunchengyang515 Nov 18, 2023
a2410da
WIP: return meaningful query results
yunchengyang515 Nov 18, 2023
83a3c58
use deno pgClient to get the correct result
yunchengyang515 Nov 18, 2023
78f892d
code cleanup
yunchengyang515 Nov 18, 2023
23846e3
remove unused import
yunchengyang515 Nov 18, 2023
417bbc9
Merge pull request #8 from stackql/dev/server-mode
yunchengyang515 Nov 18, 2023
74cfddd
wip: apply auth as param
yunchengyang515 Nov 19, 2023
64d1d39
removed auth string passing
yunchengyang515 Nov 19, 2023
f94df2b
Merge pull request #9 from stackql/dev/auth-params
yunchengyang515 Nov 19, 2023
c31ee4f
added tests for adding params and osUtils
yunchengyang515 Nov 25, 2023
eeb2931
fix tests
yunchengyang515 Nov 25, 2023
418e596
rename runClit to runCommand
yunchengyang515 Nov 25, 2023
eaf637a
Merge pull request #10 from stackql/dev/cli-properties
yunchengyang515 Nov 25, 2023
ff55c44
added proxy properties
yunchengyang515 Nov 25, 2023
0d2df97
created and added tests for upgrade
yunchengyang515 Nov 26, 2023
46ec053
test format fix
yunchengyang515 Nov 27, 2023
7f9aed5
fix spy issue in test
yunchengyang515 Nov 27, 2023
281f84a
wip: fix unpacker test in darwin
yunchengyang515 Nov 27, 2023
1e778ca
fix downloader tests with darwin
yunchengyang515 Dec 2, 2023
99f9924
Merge pull request #12 from stackql/dev/upgrade
yunchengyang515 Dec 3, 2023
a93a532
added csv and json output for run query on instnace
yunchengyang515 Dec 3, 2023
6ac3188
added new deno task runner for test
yunchengyang515 Dec 3, 2023
9574a59
added new deno task runner for test
yunchengyang515 Dec 3, 2023
ab25c55
wip: debugging failed test
yunchengyang515 Dec 4, 2023
6310cee
Merge branch 'release/v0.0.1' of https://github.com/stackql/stackqljs…
yunchengyang515 Dec 4, 2023
c4d90cd
Merge pull request #13 from stackql/dev/output-format
yunchengyang515 Dec 7, 2023
cf11480
added editor format on save
yunchengyang515 Dec 9, 2023
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
Prev Previous commit
Next Next commit
added tests for adding params and osUtils
  • Loading branch information
yunchengyang515 committed Nov 25, 2023
commit c31ee4f8d1466fc9abff031eb40bbb457f9ed592
4 changes: 2 additions & 2 deletions src/services/downloader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from "https://deno.land/std@0.133.0/path/mod.ts";
import { SupportedOs } from "../types/platforms.ts";
import { darwinUnpack, unzip } from "./unpacker.ts";
import { chomod } from "../utils/os.ts";
import osUtils from "../utils/os.ts";

export class Downloader {
private os: string;
Expand Down Expand Up @@ -123,7 +123,7 @@ export class Downloader {
}
private async setExecutable(binaryPath: string) {
const allowExecOctal = 0o755;
await chomod(binaryPath, allowExecOctal);
await osUtils.chomod(binaryPath, allowExecOctal);
}
/**
* Setup stackql binary, check if binary exists, if not download it
Expand Down
58 changes: 54 additions & 4 deletions src/stackql.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { assertStringIncludes } from "https://deno.land/std@0.206.0/assert/mod.ts";
import { StackQL } from "./stackql.ts";
import { removeStackQLDownload, startStackQLServer } from "../testing/utils.ts";
import { startStackQLServer } from "../testing/utils.ts";
import {
assertEquals,
assertExists,
} from "https://deno.land/std@0.160.0/testing/asserts.ts";
import { Downloader } from "./services/downloader.ts";
import {
assertSpyCall,
spy,
} from "https://deno.land/std@0.207.0/testing/mock.ts";
import osUtils from "./utils/os.ts";
import { assert } from "https://deno.land/std@0.133.0/_util/assert.ts";

const downloader = new Downloader();

const setupStackQL = async () => {
await downloader.setupStackQL();
};

Deno.test("StackQL CLI run query", async () => {
await setupStackQL();

Deno.test("StackQL runQuery - Successful Execution", async () => {
// Arrange
await removeStackQLDownload();
const stackQL = new StackQL();
await stackQL.initialize({ serverMode: false });

const pullQuery = "REGISTRY PULL github;";
const providerQuery = "SHOW PROVIDERS";
const githubTestQuery =
Expand All @@ -28,7 +43,42 @@ Deno.test("StackQL runQuery - Successful Execution", async () => {
assertStringIncludes(githubResult, "stackql");
});

Deno.test("StackQL runServerQuery - Successful Execution", async () => {
Deno.test("Set properties from configs", async () => {
await setupStackQL();
const runCliSpy = spy(osUtils, "runCli");
const stackQL = new StackQL();
await stackQL.initialize({
serverMode: false,
maxResults: 100,
pageLimit: 10,
maxDepth: 5,
apiTimeout: 5000,
});
const githubTestQuery =
`SELECT id, name from github.repos.repos where org='stackql'`;

await stackQL.runQuery(githubTestQuery);

const params = stackQL.getParams();
assertEquals(params.length, 8);
assertEquals(params, [
"--http.response.maxResults",
"100",
"--http.response.pageLimit",
"10",
"--indirect.depth.max",
"5",
"--apirequesttimeout",
"5000",
]);
const binaryPath = stackQL.getBinaryPath();
assert(binaryPath);
assertSpyCall(runCliSpy, 0, {
args: [binaryPath, ["exec", githubTestQuery, ...params]],
});
});

Deno.test("StackQL runServerQuery", async () => {
const { closeProcess } = await startStackQLServer();
const stackQL = new StackQL();

Expand Down
64 changes: 47 additions & 17 deletions src/stackql.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { assertExists } from "https://deno.land/std@0.206.0/assert/assert_exists.ts";
import { Downloader } from "./services/downloader.ts";
import { fileExists } from "./utils/os.ts";
import osUtils from "./utils/os.ts";
import { Server } from "./services/server.ts";
import { Client } from "https://deno.land/x/postgres@v0.17.0/client.ts";

export interface StackQLConfig {
binaryPath?: string;
serverMode?: boolean;
connectionString?: string;
maxResults?: number;
pageLimit?: number;
maxDepth?: number;
apiTimeout?: number;
}

export class StackQL {
Expand All @@ -19,40 +23,66 @@ export class StackQL {
private params: string[] = [];
constructor() {
}

getParams() {
return this.params;
}

getBinaryPath() {
return this.binaryPath;
}

public async initialize(config: StackQLConfig) {
this.binaryPath = config.binaryPath;
this.serverMode = config.serverMode || false;
if (this.serverMode) {
await this.setupConnection(config.connectionString);
return;
}
if (this.binaryPath && fileExists(this.binaryPath)) {
if (this.binaryExist()) {
return;
}
this.binaryPath = await this.downloader.setupStackQL();
this.setProperties(config);
}
private binaryExist() {
return !!this.binaryPath && osUtils.fileExists(this.binaryPath);
}

private setProperties(config: StackQLConfig): void {
if (config.maxResults !== undefined) {
this.params.push("--http.response.maxResults");
this.params.push(config.maxResults.toString());
}

if (config.pageLimit !== undefined) {
this.params.push("--http.response.pageLimit");
this.params.push(config.pageLimit.toString());
}

if (config.maxDepth !== undefined) {
this.params.push("--indirect.depth.max");
this.params.push(config.maxDepth.toString());
}

this.params.push("--apirequesttimeout");
if (config.apiTimeout !== undefined) {
this.params.push(config.apiTimeout.toString());
}
}

public async runQuery(query: string) {
assertExists(this.binaryPath);
const args = ["exec", query].concat(this.params);
const process = new Deno.Command(this.binaryPath, {
args,
stdout: "piped",
stderr: "piped",
});

const { code, stdout, stderr } = await process.output();

if (code === 0) {
const output = stdout;
const result = new TextDecoder().decode(output);
try {
const result = await osUtils.runCli(this.binaryPath, args);
return result;
} else {
const errorOutput = stderr;
const errorMessage = new TextDecoder().decode(errorOutput);
throw new Error(`StackQL query failed: ${errorMessage}`);
} catch (error) {
console.error(error);
throw new Error(`StackQL query failed: ${error.message}`);
}
}

//////////////////////Server mode related methods
private async queryObjectFormat(query: string) {
assertExists(this.connection);
Expand Down
24 changes: 24 additions & 0 deletions src/utils/os.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
assertRejects,
assertStringIncludes,
} from "https://deno.land/std@0.160.0/testing/asserts.ts";
import osUtils from "./os.ts";

Deno.test("osUtils.runCli: Test Successful Execution", async () => {
const command = Deno.build.os === "windows" ? "cmd" : "echo";
const args = Deno.build.os === "windows"
? ["/c", "echo", "Hello, World!"]
: ["Hello, World!"];
const result = await osUtils.runCli(command, args);
assertStringIncludes(result.trim(), "Hello, World!");
});

Deno.test("osUtils.runCli: Test Failed Execution", () => {
const command = "invalid";
const args = Deno.build.os === "windows"
? ["/c", "echo", "Hello, World!"]
: ["Hello, World!"];
assertRejects(async () => {
await osUtils.runCli(command, args);
});
});
32 changes: 30 additions & 2 deletions src/utils/os.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const fileExists = (path?: string) => {
const fileExists = (path?: string) => {
if (!path) {
return false;
}
Expand All @@ -13,8 +13,36 @@ export const fileExists = (path?: string) => {
}
};

export const chomod = async (path: string, mode: number) => {
const chomod = async (path: string, mode: number) => {
if (Deno.build.os !== "windows") {
await Deno.chmod(path, mode);
}
};

const runCli = async (path: string, args: string[]) => {
const process = new Deno.Command(path, {
args,
stdout: "piped",
stderr: "piped",
});

const { code, stdout, stderr } = await process.output();

if (code === 0) {
const output = stdout;
const result = new TextDecoder().decode(output);
return result;
} else {
const errorOutput = stderr;
const errorMessage = new TextDecoder().decode(errorOutput);
throw new Error(errorMessage);
}
};

const osUtils = {
fileExists,
chomod,
runCli,
};

export default osUtils;