From e3a3c37bc52e9308388616d946d922ded3c1c335 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Fri, 26 May 2023 19:02:50 -0700 Subject: [PATCH 1/3] Bumped test libs - setup CI for Sonar coverage --- .github/workflows/master.yml | 9 ++++-- .gitignore | 4 ++- sonar-project.properties | 4 +++ src/lib/cache.ts | 62 +++++++++++++++++++++++++----------- src/skimming.ts | 2 +- test/cache_test.ts | 2 +- test/mod_test.ts | 2 +- 7 files changed, 60 insertions(+), 25 deletions(-) diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 9e5979a..1c52c6b 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -18,10 +18,10 @@ jobs: with: fetch-depth: 0 - - name: Setup Deno v1.22.1 + - name: Setup Deno v1.33.4 uses: denoland/setup-deno@v1 with: - deno-version: v1.22.1 + deno-version: v1.33.4 - name: Verify formatting run: deno fmt mod.ts src/ --check @@ -30,7 +30,10 @@ jobs: run: deno lint - name: Run tests - run: deno test -A --unstable + run: deno test -A --unstable --coverage=cov_profile + + - name: Generate coverage reports + run: deno coverage cov_profile --lcov > cov_profile.lcov - name: SonarCloud Scan uses: sonarsource/sonarcloud-github-action@master diff --git a/.gitignore b/.gitignore index 84451e7..ff2982e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.vscode dist -.scannerwork \ No newline at end of file +.scannerwork +cov_profile +cov_profile.lcov \ No newline at end of file diff --git a/sonar-project.properties b/sonar-project.properties index 40f9ffe..674f8af 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,10 +1,14 @@ sonar.projectKey=petruki_skimming sonar.projectName=skimming sonar.organization=petruki +sonar.projectVersion=1.0.9 + +sonar.javascript.lcov.reportPaths=cov_profile.lcov sonar.sources=src sonar.tests=test sonar.language=typescript +sonar.exclusions=example/** sonar.dynamicAnalysis=reuseReports # Encoding of the source code. Default is default system encoding diff --git a/src/lib/cache.ts b/src/lib/cache.ts index 1e11eb0..5cab036 100644 --- a/src/lib/cache.ts +++ b/src/lib/cache.ts @@ -27,24 +27,12 @@ export default class CacheHandler { ): Output[] { const { ignoreCase, previewLength, trimContent } = options; - const result = this.cache.filter((storedData) => { - if (storedData.query.length <= query.length) { - if (ignoreCase) { - if (!query.toLowerCase().startsWith(storedData.query.toLowerCase())) { - return false; - } - } - - if (query.startsWith(storedData.query) && storedData.exp > Date.now()) { - return !this.checkOptions(storedData, { - previewLength, - ignoreCase, - trimContent, - }); - } - } - return false; - }); + const result = this.fetchCache( + query, + ignoreCase, + previewLength, + trimContent, + ); if (result.length) { const cachedResult = result[0]; @@ -68,6 +56,9 @@ export default class CacheHandler { return []; } + /** + * Stores result in memory + */ store( query: string, output: Output, @@ -107,6 +98,41 @@ export default class CacheHandler { } } + /** + * Fetches cache based on query input and options provided + * + * @param query + * @param ignoreCase + * @param previewLength + * @param trimContent + * @returns + */ + private fetchCache( + query: string, + ignoreCase: boolean | undefined, + previewLength: number | undefined, + trimContent: boolean | undefined, + ) { + return this.cache.filter((storedData) => { + if (storedData.query.length <= query.length) { + if (ignoreCase) { + if (!query.toLowerCase().startsWith(storedData.query.toLowerCase())) { + return false; + } + } + + if (query.startsWith(storedData.query) && storedData.exp > Date.now()) { + return !this.checkOptions(storedData, { + previewLength, + ignoreCase, + trimContent, + }); + } + } + return false; + }); + } + /** * Verifies if options has been changed, if so it will gather the content from the source again * diff --git a/src/skimming.ts b/src/skimming.ts index 1643b02..c7a8930 100644 --- a/src/skimming.ts +++ b/src/skimming.ts @@ -155,7 +155,7 @@ export class Skimming { */ private async readDocument(url: string, doc: string): Promise { const result = await fetch(`${url}${doc}`); - if (result != null && result.body != null) { + if (result?.body != null) { if (result.status === 200) { return await result.text().then((data: string) => { return data; diff --git a/test/cache_test.ts b/test/cache_test.ts index 6de2951..e8aabf5 100644 --- a/test/cache_test.ts +++ b/test/cache_test.ts @@ -1,4 +1,4 @@ -import { assertEquals } from "https://deno.land/std@0.142.0/testing/asserts.ts"; +import { assertEquals } from "https://deno.land/std@0.188.0/testing/asserts.ts"; import CacheHandle from "../src/lib/cache.ts"; const { test } = Deno; diff --git a/test/mod_test.ts b/test/mod_test.ts index fa80049..6fe423e 100644 --- a/test/mod_test.ts +++ b/test/mod_test.ts @@ -2,7 +2,7 @@ import { assertEquals, assertNotEquals, assertThrows, -} from "https://deno.land/std@0.142.0/testing/asserts.ts"; +} from "https://deno.land/std@0.188.0/testing/asserts.ts"; import { Context, Skimming } from "../mod.ts"; import { InvalidContext } from "../src/lib/exceptions.ts"; From 688839814c71a901eb2c57370e88819cc2acc7d4 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Fri, 26 May 2023 19:04:16 -0700 Subject: [PATCH 2/3] Fixed lint issue --- src/lib/cache.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib/cache.ts b/src/lib/cache.ts index 5cab036..eca524a 100644 --- a/src/lib/cache.ts +++ b/src/lib/cache.ts @@ -100,12 +100,12 @@ export default class CacheHandler { /** * Fetches cache based on query input and options provided - * - * @param query - * @param ignoreCase - * @param previewLength - * @param trimContent - * @returns + * + * @param query + * @param ignoreCase + * @param previewLength + * @param trimContent + * @returns */ private fetchCache( query: string, From 85cc59da48634f981ea3101991b4e55970f464ee Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Fri, 26 May 2023 19:31:29 -0700 Subject: [PATCH 3/3] Updated CI and Deno tasks --- .github/workflows/master.yml | 13 ++++++++----- .gitignore | 3 +-- .vscode/settings.json | 11 +++++++++++ deno.jsonc | 9 +++++++++ deno.lock | 9 +++++++++ sonar-project.properties | 2 +- test/cache_test.ts | 2 +- test/deps.ts | 5 +++++ test/mod_test.ts | 2 +- 9 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 deno.jsonc create mode 100644 deno.lock create mode 100644 test/deps.ts diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 1c52c6b..4ddf8ca 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -23,17 +23,20 @@ jobs: with: deno-version: v1.33.4 + - name: Setup LCOV + run: sudo apt install -y lcov + - name: Verify formatting - run: deno fmt mod.ts src/ --check + run: deno task fmt - name: Run linter run: deno lint - - name: Run tests - run: deno test -A --unstable --coverage=cov_profile + - name: Run tests and coverage + run: deno task cover - - name: Generate coverage reports - run: deno coverage cov_profile --lcov > cov_profile.lcov + - name: Fix LCOV output for SonarCloud + run: sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' coverage/report.lcov - name: SonarCloud Scan uses: sonarsource/sonarcloud-github-action@master diff --git a/.gitignore b/.gitignore index ff2982e..79b3e3b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ -*.vscode dist .scannerwork -cov_profile +coverage cov_profile.lcov \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..624d84c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "deno.enable": true, + "deno.enablePaths": [], + "deno.unstable": true, + "[typescript]": { + "editor.defaultFormatter": "vscode.typescript-language-features" + }, + "[typescriptreact]": { + "editor.defaultFormatter": "denoland.vscode-deno" + } +} \ No newline at end of file diff --git a/deno.jsonc b/deno.jsonc new file mode 100644 index 0000000..52c94ab --- /dev/null +++ b/deno.jsonc @@ -0,0 +1,9 @@ +{ + "tasks": { + "fmt": "deno fmt mod.ts src/ --check", + "test": "deno test --unstable --allow-read --allow-net --coverage=coverage", + "lcov": "deno coverage coverage --lcov --output=coverage/report.lcov", + "cover": "deno task clean && deno task test && deno task lcov && genhtml -o coverage/html coverage/report.lcov", + "clean": "rm -rf ./npm ./coverage" + } +} diff --git a/deno.lock b/deno.lock new file mode 100644 index 0000000..61edcb4 --- /dev/null +++ b/deno.lock @@ -0,0 +1,9 @@ +{ + "version": "2", + "remote": { + "https://deno.land/std@0.188.0/fmt/colors.ts": "d67e3cd9f472535241a8e410d33423980bec45047e343577554d3356e1f0ef4e", + "https://deno.land/std@0.188.0/testing/_diff.ts": "1a3c044aedf77647d6cac86b798c6417603361b66b54c53331b312caeb447aea", + "https://deno.land/std@0.188.0/testing/_format.ts": "a69126e8a469009adf4cf2a50af889aca364c349797e63174884a52ff75cf4c7", + "https://deno.land/std@0.188.0/testing/asserts.ts": "e16d98b4d73ffc4ed498d717307a12500ae4f2cbe668f1a215632d19fcffc22f" + } +} diff --git a/sonar-project.properties b/sonar-project.properties index 674f8af..058c31e 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -3,7 +3,7 @@ sonar.projectName=skimming sonar.organization=petruki sonar.projectVersion=1.0.9 -sonar.javascript.lcov.reportPaths=cov_profile.lcov +sonar.javascript.lcov.reportPaths=coverage/report.lcov sonar.sources=src sonar.tests=test diff --git a/test/cache_test.ts b/test/cache_test.ts index e8aabf5..998567f 100644 --- a/test/cache_test.ts +++ b/test/cache_test.ts @@ -1,4 +1,4 @@ -import { assertEquals } from "https://deno.land/std@0.188.0/testing/asserts.ts"; +import { assertEquals } from "./deps.ts"; import CacheHandle from "../src/lib/cache.ts"; const { test } = Deno; diff --git a/test/deps.ts b/test/deps.ts new file mode 100644 index 0000000..2c5275a --- /dev/null +++ b/test/deps.ts @@ -0,0 +1,5 @@ +export { + assertEquals, + assertNotEquals, + assertThrows, + } from "https://deno.land/std@0.188.0/testing/asserts.ts"; \ No newline at end of file diff --git a/test/mod_test.ts b/test/mod_test.ts index 6fe423e..3a843ae 100644 --- a/test/mod_test.ts +++ b/test/mod_test.ts @@ -2,7 +2,7 @@ import { assertEquals, assertNotEquals, assertThrows, -} from "https://deno.land/std@0.188.0/testing/asserts.ts"; +} from "./deps.ts"; import { Context, Skimming } from "../mod.ts"; import { InvalidContext } from "../src/lib/exceptions.ts";