Skip to content

Commit

Permalink
Bumped test libs - setup CI for Sonar coverage (#8)
Browse files Browse the repository at this point in the history
* Bumped test libs - setup CI for Sonar coverage
* Fixed lint issue
* Updated CI and Deno tasks
  • Loading branch information
petruki authored May 27, 2023
1 parent d7cb7d1 commit ff2f74f
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 28 deletions.
16 changes: 11 additions & 5 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.vscode
dist
.scannerwork
.scannerwork
coverage
cov_profile.lcov
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
9 changes: 9 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -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"
}
}
9 changes: 9 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
62 changes: 44 additions & 18 deletions src/lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -68,6 +56,9 @@ export default class CacheHandler {
return [];
}

/**
* Stores result in memory
*/
store(
query: string,
output: Output,
Expand Down Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion src/skimming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class Skimming {
*/
private async readDocument(url: string, doc: string): Promise<string> {
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;
Expand Down
2 changes: 1 addition & 1 deletion test/cache_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { assertEquals } from "./deps.ts";
import CacheHandle from "../src/lib/cache.ts";

const { test } = Deno;
Expand Down
5 changes: 5 additions & 0 deletions test/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {
assertEquals,
assertNotEquals,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
2 changes: 1 addition & 1 deletion test/mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
assertEquals,
assertNotEquals,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
} from "./deps.ts";
import { Context, Skimming } from "../mod.ts";
import { InvalidContext } from "../src/lib/exceptions.ts";

Expand Down

0 comments on commit ff2f74f

Please sign in to comment.