Skip to content

Commit

Permalink
tests: add tests for utils
Browse files Browse the repository at this point in the history
  • Loading branch information
TorstenDittmann committed Sep 4, 2023
1 parent de4eacc commit e69924c
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/demo/src/lib/Nodes.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script context="module">
export { default as Heading } from './nodes/Heading.svelte';
export { default as Fence } from './nodes/Fence.svelte';
</script>
</script>
2 changes: 1 addition & 1 deletion apps/demo/src/lib/Tags.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
export { default as CTA } from './tags/CTA.svelte';
export { default as Matrix } from './tags/Matrix.svelte';
export { default as Matrix_Item } from './tags/MatrixItem.svelte';
</script>
</script>
2 changes: 1 addition & 1 deletion apps/demo/src/lib/layouts/Default.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
export let title: string|null = null;
export let title: string | null = null;
</script>

<svelte:head>
Expand Down
4 changes: 3 additions & 1 deletion apps/demo/src/lib/nodes/Fence.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
export let language: string;
export let process: boolean;
const result = process ? hljs.highlight(content, { language: language ?? 'sh' }) : { value: content };
const result = process
? hljs.highlight(content, { language: language ?? 'sh' })
: { value: content };
</script>

<!-- eslint-disable-next-line svelte/no-at-html-tags -->
Expand Down
3 changes: 2 additions & 1 deletion apps/demo/src/lib/tags/CTA.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<slot />
<ul class="p-inline-list u-no-margin--bottom">
<li class="p-inline-list__item">
<a href="/documentation" class="p-button--positive u-no-margin--bottom">Get started</a
<a href="/documentation" class="p-button--positive u-no-margin--bottom"
>Get started</a
>
</li>
<li class="p-inline-list__item">
Expand Down
2 changes: 1 addition & 1 deletion packages/process/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
getNameOfDeclaration,
isVariableStatement,
} from 'typescript';
import { dirname, join, relative } from 'path';
import { dirname, join } from 'path';
import { load as loadYaml } from 'js-yaml';
import { parse as svelteParse, walk } from 'svelte/compiler';
import { render_html } from './renderer';
Expand Down
11 changes: 7 additions & 4 deletions packages/process/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import { sep as posix_sep } from 'path/posix';

export function get_all_files(path: string): string[] {
const files = [];

for (const file of readdirSync(path)) {
const fullPath = path + '/' + file;
if (lstatSync(fullPath).isDirectory())
get_all_files(fullPath).forEach((x) => files.push(file + '/' + x));
else files.push(file);
const fullPath = join(path, file);
if (lstatSync(fullPath).isDirectory()) {
get_all_files(fullPath).forEach((x) => files.push(join(file, x)));
} else {
files.push(file);
}
}
return files;
}
Expand Down
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions packages/process/tests/fixtures/read_file/file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lorem ipsum
1 change: 1 addition & 0 deletions packages/process/tests/fixtures/write_to_file/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file
71 changes: 71 additions & 0 deletions packages/process/tests/utils.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
import {
relative_posix_path,
read_file,
get_all_files,
path_exists,
write_to_file,
} from '../dist/utils.js';

test('relative_posix_path', async () => {
assert.equal(
relative_posix_path('/test/a/b/c', '/test/file.js'),
'../../file.js',
);
});

test('get_all_files', async (context) => {
await context.test('works', () => {
const files = get_all_files('tests/fixtures/get_all_files');
assert.ok(files.includes('file'));
assert.ok(files.includes('nested/file'));
assert.equal(files.length, 2);
});
await context.test('throws exception when directory does not exist', () => {
try {
get_all_files('tests/fixtures/get_all_files/unknown');
assert.fail();
} catch (error) {
assert.ok(true);
}
});
});

test('read_file', async (context) => {
await context.test('works', () => {
const content = read_file('tests/fixtures/read_file/file');
assert.equal(content, 'lorem ipsum');
});
await context.test('throws exception when file does not exist', () => {
try {
read_file('tests/fixtures/read_file/unknown');
assert.fail('should throw when file does not exist');
} catch (error) {
assert.ok(true);
}
});
});

test('path_exists', async () => {
assert.ok(path_exists('tests/fixtures/path_exists'));
assert.ok(!path_exists('tests/fixtures/path_exists/unknown'));
});

test('write_to_file', async (context) => {
await context.test('works', () => {
write_to_file('tests/fixtures/write_to_file/file', 'lorem ipsum');
assert.equal(
read_file('tests/fixtures/write_to_file/file'),
'lorem ipsum',
);
});
await context.test('can overwrite', () => {
write_to_file('tests/fixtures/write_to_file/file', 'lorem ipsum');
write_to_file('tests/fixtures/write_to_file/file', 'overwritten');
assert.equal(
read_file('tests/fixtures/write_to_file/file'),
'overwritten',
);
});
});

0 comments on commit e69924c

Please sign in to comment.