From 8013d999b9b92211761f207b3e724d5305284f68 Mon Sep 17 00:00:00 2001
From: Harold Hunt <harold@pwrdrvr.com>
Date: Thu, 23 May 2024 09:23:43 -0400
Subject: [PATCH] Fix dist references from TS tests

---
 lib/xmllint.ts               | 22 +++++++++++++++++++++-
 tests/sitemap-parser.test.ts |  2 +-
 tests/xmllint.test.ts        |  2 +-
 3 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/lib/xmllint.ts b/lib/xmllint.ts
index fd51d8e9..4e997ad9 100644
--- a/lib/xmllint.ts
+++ b/lib/xmllint.ts
@@ -1,7 +1,27 @@
+import { existsSync } from 'fs';
 import { Readable } from 'stream';
 import { resolve } from 'path';
 import { execFile } from 'child_process';
 import { XMLLintUnavailable } from './errors';
+
+/**
+ * Finds the `schema` directory since we may be located in
+ * `lib` or `dist/lib` when this is called.
+ *
+ * @throws {Error} if the schema directory is not found
+ * @returns {string} the path to the schema directory
+ */
+function findSchemaDir(): string {
+  const paths = ['.', '..', '../..'];
+  for (const p of paths) {
+    const schemaPath = resolve(p, 'schema');
+    if (existsSync(schemaPath)) {
+      return schemaPath;
+    }
+  }
+  throw new Error('Schema directory not found');
+}
+
 /**
  * Verify the passed in xml is valid. Requires xmllib be installed
  * @param xml what you want validated
@@ -10,7 +30,7 @@ import { XMLLintUnavailable } from './errors';
 export function xmlLint(xml: string | Readable): Promise<void> {
   const args = [
     '--schema',
-    resolve(__dirname, '..', '..', 'schema', 'all.xsd'),
+    resolve(findSchemaDir(), 'all.xsd'),
     '--noout',
     '-',
   ];
diff --git a/tests/sitemap-parser.test.ts b/tests/sitemap-parser.test.ts
index 3bb34926..ee39373a 100644
--- a/tests/sitemap-parser.test.ts
+++ b/tests/sitemap-parser.test.ts
@@ -7,7 +7,7 @@ import {
   XMLToSitemapItemStream,
   ObjectStreamToJSON,
 } from '../lib/sitemap-parser';
-import { SitemapStreamOptions } from '../dist';
+import { SitemapStreamOptions } from '../lib/sitemap-stream';
 import { ErrorLevel, SitemapItem } from '../lib/types';
 const pipeline = promisify(pipe);
 // eslint-disable-next-line @typescript-eslint/no-var-requires
diff --git a/tests/xmllint.test.ts b/tests/xmllint.test.ts
index c8f1321b..78664d80 100644
--- a/tests/xmllint.test.ts
+++ b/tests/xmllint.test.ts
@@ -1,4 +1,4 @@
-import { xmlLint } from '../dist/index';
+import { xmlLint } from '../lib/xmllint';
 // eslint-disable-next-line @typescript-eslint/no-var-requires
 const execFileSync = require('child_process').execFileSync;
 let hasXMLLint = true;