Skip to content

Commit 457ac31

Browse files
authored
Merge pull request #22 from stackql/feature/show-rendered-query
dryrun to show rendered query
2 parents 7fd727f + eae10df commit 457ac31

File tree

5 files changed

+72
-22
lines changed

5 files changed

+72
-22
lines changed

.github/workflows/stackql-assert.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
#
6161
# Example `test_query_file_path` with `expected_rows` supplying `vars` using `jsonnet` config provided using `data_file_path`
6262
#
63-
- name: Use test query string and expected results file, with auth object
63+
- name: Use test query file with a jsonnet data file sourcing external vars and an expected row count
6464
uses: ./
6565
with:
6666
test_query_file_path: './.github/workflows/workflow_scripts/github-example.iql'

action.yml

+22-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ inputs:
3333
runs:
3434
using: "composite"
3535
steps:
36-
- name: Check StackQL is installed and set output
36+
- name: check if stackql is installed and set output
3737
id: check-stackql
3838
shell: bash
3939
run: |
@@ -42,15 +42,15 @@ runs:
4242
else
4343
echo "stackql_installed=false" >> $GITHUB_OUTPUT
4444
fi
45-
4645
47-
- name: Setup StackQL
46+
- name: setup stackql
4847
uses: stackql/[email protected]
4948
if: ${{steps.check-stackql.outputs.stackql_installed == 'false'}}
5049
with:
5150
use_wrapper: true
5251

53-
- name: Setup auth
52+
- name: setup auth
53+
if: (inputs.auth_obj_path != '') || (inputs.auth_str != '')
5454
id: setup-auth
5555
uses: actions/github-script@v6
5656
with:
@@ -77,7 +77,24 @@ runs:
7777
DATA_FILE_PATH: ${{inputs.data_file_path}}
7878
VARS: ${{inputs.vars}}
7979
OUTPUT: 'json'
80-
80+
81+
- name: dryrun stackql command
82+
id: dryrun-query
83+
shell: bash
84+
run: |
85+
${{ env.STACKQL_DRYRUN_COMMAND }}
86+
87+
- name: show rendered stackql query
88+
uses: actions/github-script@v6
89+
with:
90+
script: |
91+
const path = require('path');
92+
const utilsPath = path.join(process.env.GITHUB_ACTION_PATH, 'lib', 'utils.js')
93+
const {showStackQLQuery} = require(utilsPath)
94+
showStackQLQuery(core)
95+
env:
96+
DRYRUN_RESULT: ${{steps.dryrun-query.outputs.stdout}}
97+
8198
- name: execute stackql command
8299
id: exec-query
83100
shell: bash
@@ -97,9 +114,6 @@ runs:
97114
EXPECTED_RESULTS_STR: ${{ inputs.expected_results_str }}
98115
EXPECTED_RESULTS_FILE_PATH: ${{inputs.expected_results_file_path}}
99116
EXPECTED_ROWS: ${{inputs.expected_rows}}
100-
101-
102-
103117

104118
branding:
105119
icon: 'terminal'

lib/assert.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const parseResult = (resultStr, varName) => {
1010
const jsonObj = JSON.parse(jsonStr); // parse the JSON string into an object
1111
return jsonObj;
1212
} catch (error) {
13-
throw(`Failed to parse ${varName} JSON
13+
throw(`Failed to parse ${varName} JSON
1414
\nvalue: ${resultStr}
1515
\nerror: ${error}`);
1616
}
@@ -34,18 +34,21 @@ const checkResult = (expectedResult, expectedRows, actualResult) => {
3434
let equality;
3535
let message;
3636
expectedRows = parseInt(expectedRows);
37+
const successMessage = `✅ StackQL Assert Successful`;
38+
const failureMessage = `❌ StackQL Assert Failed`;
39+
3740
// if only passed expectedRows, check expectedRows
3841
// if only passed expected result, only check expected result
3942
// if both passed, check both
4043
if (expectedRows) {
4144
equality = actualResult.length === expectedRows;
4245
if(equality) {
43-
message = `============ StackQL Assert Successful (row count) ============ \n
46+
message = `============ ${successMessage} (row count) ============ \n
4447
Expected Number of Rows: ${expectedRows} \n
4548
Actual Number of Rows: ${actualResult.length} \n
4649
`;
4750
} else {
48-
message = `============ StackQL Assert Failed (row count) ============ \n
51+
message = `============ ${failureMessage} (row count) ============ \n
4952
Expected Number of Rows: ${expectedRows} \n
5053
Actual Number of Rows: ${actualResult.length} \n
5154
Execution Result: ${JSON.stringify(actualResult)} \n
@@ -56,9 +59,9 @@ const checkResult = (expectedResult, expectedRows, actualResult) => {
5659
if (expectedResult) {
5760
equality = JSON.stringify(expectedResult) === JSON.stringify(actualResult);
5861
if(equality) {
59-
message = `============ StackQL Assert Successful (expected results) ============`;
62+
message = `============ ${successMessage} (expected results) ============`;
6063
} else {
61-
message = `============ StackQL Assert Failed (expected results) ============ \n
64+
message = `============ ${failureMessage} (expected results) ============ \n
6265
Expected: ${JSON.stringify(expectedResult)}\n
6366
Actual: ${JSON.stringify(actualResult)}
6467
`;
@@ -80,7 +83,7 @@ function checkParameters(expectedResultStr, expectedResultFilePath, expectedRows
8083
.map(param => param.name)
8184

8285
if (missingParams.length === 3) {
83-
const errorMessage = "Cannot find expected result, file path or expected rows";
86+
const errorMessage = "Cannot find expected result, file path or expected rows";
8487
throw errorMessage;
8588
}
8689
}
@@ -111,7 +114,7 @@ const assertResult = (coreObj) =>{
111114
const actualResult = parseResult(execResultStr);
112115

113116
if (!actualResult) {
114-
core.setFailed("No Output from executing query");
117+
core.setFailed(`❌ No Output from executing query`);
115118
}
116119

117120
const {equality, message} = checkResult(expectedResult, expectedRows, actualResult);

lib/tests/assert.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe('getExpectedResult', ()=>{
133133

134134
assertResult(coreObj)
135135

136-
expect(coreObj.setFailed).toHaveBeenCalledWith('Cannot find expected result, file path or expected rows')
136+
expect(coreObj.setFailed).toHaveBeenCalledWith('Cannot find expected result, file path or expected rows')
137137
});
138138

139139
it('it should setFailed when actual result is not equal to expected result', () => {

lib/utils.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ function setupAuth(core) {
2828
core.exportVariable("AUTH", auth);
2929
}
3030

31+
async function showStackQLQuery(core) {
32+
try {
33+
let [
34+
dryRunCommand,
35+
dryRunResult,
36+
] = [
37+
process.env.STACKQL_DRYRUN_COMMAND,
38+
process.env.DRYRUN_RESULT,
39+
];
40+
41+
if (!dryRunResult) {
42+
core.setFailed("No Dryrun Output from stackql command");
43+
}
44+
45+
core.info(`\n🚀 rendered stackql query:\n${dryRunResult}`);
46+
47+
} catch (e) {
48+
core.setFailed(e);
49+
}
50+
}
51+
3152
async function getStackqlCommand(core) {
3253

3354
const [query, queryFilePath, dataFilePath, vars, auth, output = "json"] = [
@@ -45,32 +66,43 @@ async function getStackqlCommand(core) {
4566
}
4667

4768
let args = ["exec"];
69+
let dryRunArgs = ["exec", "-H"];
4870

4971
if (query) {
5072
args.push(`"${query}"`);
73+
dryRunArgs.push(`"${query}"`);
5174
} else {
52-
args.push("-i", queryFilePath);
75+
args.push("-i", queryFilePath);
76+
dryRunArgs.push("-i", queryFilePath);
5377
}
54-
78+
5579
if (checkEnvVarValid(dataFilePath)) {
5680
args.push(`--iqldata='${dataFilePath}'`);
81+
dryRunArgs.push(`--iqldata='${dataFilePath}'`);
5782
}
58-
83+
5984
if (checkEnvVarValid(vars)) {
6085
args.push(`--var='${vars}'`);
86+
dryRunArgs.push(`--var='${vars}'`);
6187
}
6288

6389
if (checkEnvVarValid(auth)) {
6490
args.push(`--auth='${auth}'`);
91+
dryRunArgs.push(`--auth='${auth}'`);
6592
}
6693

6794
args.push(`--output='${output}'`);
95+
dryRunArgs.push(`--output='text'`);
96+
dryRunArgs.push(`--dryrun`);
6897

6998
try {
70-
core.exportVariable('STACKQL_COMMAND', `stackql ${args.join(" ")}`)
99+
const stackqlQuery = `stackql ${args.join(" ")}`;
100+
const stackqlDryRunQuery = `stackql ${dryRunArgs.join(" ")}`;
101+
core.exportVariable('STACKQL_COMMAND', stackqlQuery);
102+
core.exportVariable('STACKQL_DRYRUN_COMMAND', stackqlDryRunQuery);
71103
} catch (error) {
72104
core.error(error);
73-
core.setFailed("Error when executing stackql");
105+
core.setFailed("Error exporting stackql command");
74106
}
75107
}
76108

@@ -88,4 +120,5 @@ const checkEnvVarValid = (variable) => {
88120
module.exports = {
89121
setupAuth,
90122
getStackqlCommand,
123+
showStackQLQuery,
91124
};

0 commit comments

Comments
 (0)