diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 9e5979a..4ddf8ca 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -18,19 +18,25 @@ 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: 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 + - name: Run tests and coverage + run: deno task cover + + - 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 84451e7..79b3e3b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -*.vscode dist -.scannerwork \ No newline at end of file +.scannerwork +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 40f9ffe..058c31e 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=coverage/report.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..eca524a 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..998567f 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 "./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 fa80049..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.142.0/testing/asserts.ts"; +} from "./deps.ts"; import { Context, Skimming } from "../mod.ts"; import { InvalidContext } from "../src/lib/exceptions.ts";