From c9fa6fa73818b10349c4ed4d7ebdf8bc1887b98f Mon Sep 17 00:00:00 2001 From: Matteo Cristino <102997993+matteo-cristino@users.noreply.github.com> Date: Thu, 24 Oct 2024 15:28:55 +0200 Subject: [PATCH] fix: livedirectory ignores autorun folders (#382) * 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 --- .gitignore | 4 +++ package.json | 2 +- src/directory.ts | 30 ++++++++++++------- src/index.ts | 2 +- .../.autorun/autorun_install_test.keys.json | 3 ++ .../autorun_install_test.metadata.json | 4 +++ .../.autorun/autorun_install_test.zen | 6 ++++ .../.autorun/autorun_startup_test.keys.json | 3 ++ .../autorun_startup_test.metadata.json | 4 +++ .../.autorun/autorun_startup_test.zen | 6 ++++ tests/workflow.stepci.yml | 29 +++++++++++++++++- 11 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 tests/fixtures/.autorun/autorun_install_test.keys.json create mode 100644 tests/fixtures/.autorun/autorun_install_test.metadata.json create mode 100644 tests/fixtures/.autorun/autorun_install_test.zen create mode 100644 tests/fixtures/.autorun/autorun_startup_test.keys.json create mode 100644 tests/fixtures/.autorun/autorun_startup_test.metadata.json create mode 100644 tests/fixtures/.autorun/autorun_startup_test.zen diff --git a/.gitignore b/.gitignore index 564ec8d..faebf68 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/package.json b/package.json index a5cceac..4e7407c 100644 --- a/package.json +++ b/package.json @@ -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'" }, diff --git a/src/directory.ts b/src/directory.ts index bb43824..5be8d2d 100644 --- a/src/directory.ts +++ b/src/directory.ts @@ -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'; @@ -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) } }); } diff --git a/src/index.ts b/src/index.ts index 2f3d1b6..6666d96 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); diff --git a/tests/fixtures/.autorun/autorun_install_test.keys.json b/tests/fixtures/.autorun/autorun_install_test.keys.json new file mode 100644 index 0000000..aea3089 --- /dev/null +++ b/tests/fixtures/.autorun/autorun_install_test.keys.json @@ -0,0 +1,3 @@ +{ + "file_path": "public/install.out.json" +} \ No newline at end of file diff --git a/tests/fixtures/.autorun/autorun_install_test.metadata.json b/tests/fixtures/.autorun/autorun_install_test.metadata.json new file mode 100644 index 0000000..0599c8d --- /dev/null +++ b/tests/fixtures/.autorun/autorun_install_test.metadata.json @@ -0,0 +1,4 @@ +{ + "autorun": "install", + "executed": false +} \ No newline at end of file diff --git a/tests/fixtures/.autorun/autorun_install_test.zen b/tests/fixtures/.autorun/autorun_install_test.zen new file mode 100644 index 0000000..7a79687 --- /dev/null +++ b/tests/fixtures/.autorun/autorun_install_test.zen @@ -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 \ No newline at end of file diff --git a/tests/fixtures/.autorun/autorun_startup_test.keys.json b/tests/fixtures/.autorun/autorun_startup_test.keys.json new file mode 100644 index 0000000..13144b8 --- /dev/null +++ b/tests/fixtures/.autorun/autorun_startup_test.keys.json @@ -0,0 +1,3 @@ +{ + "file_path": "public/startup.out.json" +} \ No newline at end of file diff --git a/tests/fixtures/.autorun/autorun_startup_test.metadata.json b/tests/fixtures/.autorun/autorun_startup_test.metadata.json new file mode 100644 index 0000000..d3f9857 --- /dev/null +++ b/tests/fixtures/.autorun/autorun_startup_test.metadata.json @@ -0,0 +1,4 @@ +{ + "autorun": "startup", + "other": "cool" +} diff --git a/tests/fixtures/.autorun/autorun_startup_test.zen b/tests/fixtures/.autorun/autorun_startup_test.zen new file mode 100644 index 0000000..f1e7e7b --- /dev/null +++ b/tests/fixtures/.autorun/autorun_startup_test.zen @@ -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 diff --git a/tests/workflow.stepci.yml b/tests/workflow.stepci.yml index 964144f..4537670 100644 --- a/tests/workflow.stepci.yml +++ b/tests/workflow.stepci.yml @@ -496,4 +496,31 @@ tests: method: GET check: status: 200 - sha256: 9a2270d5964f64981fb1e91dd13e5941262817bdce873cf357c92adbef906b5d \ No newline at end of file + 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\"" \ No newline at end of file