ソースを参照

Chore: add tests for ruleset create utility

SukkaW 1 年間 前
コミット
0bbbeaa372
2 ファイル変更98 行追加42 行削除
  1. 53 0
      Build/lib/create-file.test.ts
  2. 45 42
      Build/lib/create-file.ts

+ 53 - 0
Build/lib/create-file.test.ts

@@ -0,0 +1,53 @@
+import { expect } from 'chai';
+import { fileEqual } from './create-file';
+
+// eslint-disable-next-line @typescript-eslint/require-await -- async iterable
+const createSource = async function *(input: string[]) {
+  for (const line of input) {
+    yield line;
+  }
+};
+
+const test = async (a: string[], b: string[], expected: boolean) => {
+  expect((await fileEqual(a, createSource(b)))).to.eq(expected);
+};
+
+describe('fileEqual', () => {
+  it('same', () => test(
+    ['A', 'B'],
+    ['A', 'B'],
+    true
+  ));
+
+  it('ignore comment', async () => {
+    await test(
+      ['# A', 'B'],
+      ['# B', 'B'],
+      true
+    );
+
+    await test(
+      ['# A', '# C', 'B'],
+      ['# A', '# D', 'B'],
+      true
+    );
+  });
+
+  it('comment more', () => test(
+    ['# A', 'B'],
+    ['# A', '# B', 'B'],
+    false
+  ));
+
+  it('larger', () => test(
+    ['A', 'B'],
+    ['A', 'B', 'C'],
+    false
+  ));
+
+  it('smaller', () => test(
+    ['A', 'B', 'C'],
+    ['A', 'B'],
+    false
+  ));
+});

+ 45 - 42
Build/lib/create-file.ts

@@ -12,54 +12,57 @@ import { createTrie } from './trie';
 import { pack, unpackFirst, unpackSecond } from './bitwise';
 import { asyncWriteToStream } from './async-write-to-stream';
 
-export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
-  let isEqual = true;
-  const linesALen = linesA.length;
+export const fileEqual = async (linesA: string[], source: AsyncIterable<string>): Promise<boolean> => {
+  if (linesA.length === 0) {
+    return false;
+  }
 
-  if (!fs.existsSync(filePath)) {
-    console.log(`${filePath} does not exists, writing...`);
-    isEqual = false;
-  } else if (linesALen === 0) {
-    console.log(`Nothing to write to ${filePath}...`);
-    isEqual = false;
-  } else {
-    isEqual = await span.traceChildAsync(`comparing ${filePath}`, async () => {
-      let index = 0;
-      for await (const lineB of readFileByLine(filePath)) {
-        const lineA = linesA[index] as string | undefined;
-        index++;
+  let index = 0;
+  for await (const lineB of source) {
+    const lineA = linesA[index] as string | undefined;
+    index++;
 
-        if (lineA == null) {
-          // The file becomes smaller
-          return false;
-        }
+    if (lineA == null) {
+      // The file becomes smaller
+      return false;
+    }
 
-        if (lineA[0] === '#' && lineB[0] === '#') {
-          continue;
-        }
-        if (
-          lineA[0] === '/'
-          && lineA[1] === '/'
-          && lineB[0] === '/'
-          && lineB[1] === '/'
-          && lineA[3] === '#'
-          && lineB[3] === '#'
-        ) {
-          continue;
-        }
+    if (lineA[0] === '#' && lineB[0] === '#') {
+      continue;
+    }
+    if (
+      lineA[0] === '/'
+      && lineA[1] === '/'
+      && lineB[0] === '/'
+      && lineB[1] === '/'
+      && lineA[3] === '#'
+      && lineB[3] === '#'
+    ) {
+      continue;
+    }
 
-        if (lineA !== lineB) {
-          return false;
-        }
-      }
+    if (lineA !== lineB) {
+      return false;
+    }
+  }
 
-      if (index !== linesALen) {
-        // The file becomes larger
-        return false;
-      }
+  if (index !== linesA.length) {
+    // The file becomes larger
+    return false;
+  }
 
-      return true;
-    });
+  return true;
+};
+
+export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
+  let isEqual = true;
+  const linesALen = linesA.length;
+
+  if (fs.existsSync(filePath)) {
+    isEqual = await fileEqual(linesA, readFileByLine(filePath));
+  } else {
+    console.log(`${filePath} does not exists, writing...`);
+    isEqual = false;
   }
 
   if (isEqual) {