Skip to content

Commit

Permalink
fix: livedirectory ignores autorun folders (#382)
Browse files Browse the repository at this point in the history
* fix: livedirectory ignores autorun folders

* chore: sort list of apis

* test: add autorun and list of api in stepci tests

* chore: add env for testing

* chore: update gitignore with file produced by autorun tests

* test: update api list

* refactor: do not use env file for tests

* chore: add some comments to explian live-directory behavior
  • Loading branch information
matteo-cristino authored Oct 24, 2024
1 parent 193d886 commit c9fa6fa
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,7 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# produced by tests
public/install.out.json
public/startup.out.json
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"sea": "pnpm b && pnpm p",
"dev": "nodemon -e ts -w ./src -x pnpm run watch -z ./contracts -p 3000",
"watch": "node --loader ts-node/esm src/index.ts",
"test": "pnpm e2e && jest",
"test": "FILES_DIR=. pnpm e2e && jest",
"start": "node --loader ts-node/esm src/index.ts -z ./tests/fixtures -p 3000 --public-directory ./public",
"e2e": "start-server-and-test http-get://0.0.0.0:3000 'pnpm stepci run ./tests/workflow.stepci.yml'"
},
Expand Down
30 changes: 20 additions & 10 deletions src/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

import { join, resolve } from 'path';
import fs from 'fs';
import LiveDirectory from 'live-directory';
import { config } from './cli.js';
Expand All @@ -13,29 +14,38 @@ export class Directory {
private static instance: Directory;
private liveDirectory: LiveDirectory;

// This should keep track of file that ends up with:
// * .[zen]
// * .[chain].js
// * .[keys|data|metdata|schema].json
// * .conf
// that are present in the zencodeDirectory and outside of the autorun folder
private constructor() {
const autorunDir = join(resolve(config.zencodeDirectory), '.autorun');
this.liveDirectory = new LiveDirectory(config.zencodeDirectory, {
static: false,
filter: {
keep: (path) => {
keep: (path: string): boolean => {
const pathArray = path.split('.');
if (pathArray.length < 2) return false;
const ext = pathArray.pop() as string;
const secondExt = pathArray.pop() as string;
return (
FILE_EXTENSIONS.contract.includes(ext) ||
(ext === FILE_EXTENSIONS.js &&
FILE_EXTENSIONS.chain.includes(secondExt) &&
pathArray.pop()) ||
(ext === FILE_EXTENSIONS.json &&
FILE_EXTENSIONS.jsonDouble.includes(secondExt) &&
pathArray.pop()) ||
Boolean(
ext === FILE_EXTENSIONS.js &&
FILE_EXTENSIONS.chain.includes(secondExt) &&
pathArray.pop()
) ||
Boolean(
ext === FILE_EXTENSIONS.json &&
FILE_EXTENSIONS.jsonDouble.includes(secondExt) &&
pathArray.pop()
) ||
ext === FILE_EXTENSIONS.conf
);
},
ignore: {
// extensions: ['keys']
}
ignore: (path: string): boolean => path.startsWith(autorunDir)
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const ncrApp = async () => {
res
.writeStatus('200 OK')
.writeHeader('Content-Type', 'application/json')
.end(JSON.stringify(files));
.end(JSON.stringify(files.sort()));
})
.get(config.openapiPath, (res) => {
res.writeStatus('200 OK').writeHeader('Content-Type', 'text/html').end(openapiTemplate);
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/.autorun/autorun_install_test.keys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"file_path": "public/install.out.json"
}
4 changes: 4 additions & 0 deletions tests/fixtures/.autorun/autorun_install_test.metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"autorun": "install",
"executed": false
}
6 changes: 6 additions & 0 deletions tests/fixtures/.autorun/autorun_install_test.zen
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Given I have a 'string' named 'file_path'

When I write the string 'yes' in 'result'

Then print 'result'
Then I send path 'file_path' and send content 'result' and store in file
3 changes: 3 additions & 0 deletions tests/fixtures/.autorun/autorun_startup_test.keys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"file_path": "public/startup.out.json"
}
4 changes: 4 additions & 0 deletions tests/fixtures/.autorun/autorun_startup_test.metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"autorun": "startup",
"other": "cool"
}
6 changes: 6 additions & 0 deletions tests/fixtures/.autorun/autorun_startup_test.zen
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Given I have a 'string' named 'file_path'

When I write the string 'yes' in 'result'

Then print 'result'
Then I send path 'file_path' and send content 'result' and store in file
29 changes: 28 additions & 1 deletion tests/workflow.stepci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,31 @@ tests:
method: GET
check:
status: 200
sha256: 9a2270d5964f64981fb1e91dd13e5941262817bdce873cf357c92adbef906b5d
sha256: 9a2270d5964f64981fb1e91dd13e5941262817bdce873cf357c92adbef906b5d

api_list:
steps:
- name: GET request
http:
url: http://${{env.host}}/
method: GET
check:
status: 200
body: "[\"http://localhost:3000/broken\",\"http://localhost:3000/broken_contracts/exec\",\"http://localhost:3000/brokenschema\",\"http://localhost:3000/chain\",\"http://localhost:3000/chain/hello\",\"http://localhost:3000/chain/hello_with_input\",\"http://localhost:3000/hello\",\"http://localhost:3000/meta/after_precondition\",\"http://localhost:3000/meta/content_type\",\"http://localhost:3000/meta/create_http_headers_and_print\",\"http://localhost:3000/meta/get_http_headers_and_print\",\"http://localhost:3000/meta/http_headers\",\"http://localhost:3000/meta/http_headers_metadata_without_headers\",\"http://localhost:3000/meta/just_post\",\"http://localhost:3000/meta/set_http_headers_response\",\"http://localhost:3000/meta/status_code\",\"http://localhost:3000/my-keyring\",\"http://localhost:3000/valid-schema\"]"

autorun:
steps:
- name: install
http:
url: http://${{env.host}}/install.out.json
method: GET
check:
status: 200
body: "\"yes\""
- name: startup
http:
url: http://${{env.host}}/startup.out.json
method: GET
check:
status: 200
body: "\"yes\""

0 comments on commit c9fa6fa

Please sign in to comment.