From e508774d47bca28617a8862bc679f7f58c51b01c Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 16 Dec 2024 21:22:25 -0500 Subject: [PATCH] src: move test directory under src/ This commit moves the existing test/ directory under src/. The goal is to eventually support running the test suite on the transpiled code. The actual test files that leverage the test/ directory are already located in src/ anyway. --- package.json | 3 +- src/attach_test.ts | 2 +- src/azure_auth_test.ts | 2 +- src/config_test.ts | 4 +- src/cp_test.ts | 2 +- src/exec_test.ts | 2 +- src/gcp_auth_test.ts | 2 +- src/test/call-awaiter.ts | 13 ++++ {test => src/test}/echo space.js | 2 +- {test => src/test}/index.ts | 0 src/test/match-buffer.ts | 75 +++++++++++++++++++ .../resizable-writeable-stream-buffer.ts | 4 +- test/call-awaiter.ts | 13 ---- test/match-buffer.ts | 73 ------------------ tsconfig.json | 2 +- 15 files changed, 101 insertions(+), 98 deletions(-) create mode 100644 src/test/call-awaiter.ts rename {test => src/test}/echo space.js (53%) rename {test => src/test}/index.ts (100%) create mode 100644 src/test/match-buffer.ts rename {test => src/test}/resizable-writeable-stream-buffer.ts (73%) delete mode 100644 test/call-awaiter.ts delete mode 100644 test/match-buffer.ts diff --git a/package.json b/package.json index a36961ef63..976a34f3c0 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,8 @@ "exclude": [ "src/gen/*/**.ts", "src/index.ts", - "src/*_test.ts" + "src/*_test.ts", + "src/test" ], "extension": [ ".ts" diff --git a/src/attach_test.ts b/src/attach_test.ts index f857ae6725..5d64d111b0 100644 --- a/src/attach_test.ts +++ b/src/attach_test.ts @@ -3,7 +3,7 @@ import WebSocket from 'isomorphic-ws'; import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers'; import { anyFunction, anything, capture, instance, mock, verify, when } from 'ts-mockito'; -import { CallAwaiter, matchBuffer, ResizableWriteableStreamBuffer } from '../test'; +import { CallAwaiter, matchBuffer, ResizableWriteableStreamBuffer } from './test'; import { Attach } from './attach.js'; import { KubeConfig } from './config.js'; import { TerminalSize } from './terminal-size-queue.js'; diff --git a/src/azure_auth_test.ts b/src/azure_auth_test.ts index 747c2bf8ec..619e1f6278 100644 --- a/src/azure_auth_test.ts +++ b/src/azure_auth_test.ts @@ -297,7 +297,7 @@ describe('AzureAuth', () => { authProvider: { name: 'azure', config: { - 'cmd-path': join(__dirname, '..', 'test', 'echo space.js'), + 'cmd-path': join(__dirname, 'test', 'echo space.js'), 'cmd-args': `'${responseStr}'`, 'token-key': '{.token.accessToken}', 'expiry-key': '{.token.token_expiry}', diff --git a/src/config_test.ts b/src/config_test.ts index a9171d79d5..5af5326071 100644 --- a/src/config_test.ts +++ b/src/config_test.ts @@ -10,7 +10,7 @@ import mockfs from 'mock-fs'; import { Headers } from 'node-fetch'; import { HttpMethod } from './index.js'; -import { assertRequestAgentsEqual, assertRequestOptionsEqual } from '../test/match-buffer'; +import { assertRequestAgentsEqual, assertRequestOptionsEqual } from './test/match-buffer'; import { CoreV1Api, RequestContext } from './api.js'; import { bufferFromFileOrString, findHomeDir, findObject, KubeConfig, makeAbsolutePath } from './config.js'; import { ActionOnInvalid, Cluster, newClusters, newContexts, newUsers, User } from './config_types.js'; @@ -1067,7 +1067,7 @@ describe('KubeConfig', () => { authProvider: { name: 'azure', // applies to gcp too as they are both handled by CloudAuth class config: { - 'cmd-path': path.join(__dirname, '..', 'test', 'echo space.js'), + 'cmd-path': path.join(__dirname, 'test', 'echo space.js'), 'cmd-args': `'${responseStr}'`, 'token-key': '{.token.accessToken}', 'expiry-key': '{.token.token_expiry}', diff --git a/src/cp_test.ts b/src/cp_test.ts index 4dd2ecac04..eeee0342f0 100644 --- a/src/cp_test.ts +++ b/src/cp_test.ts @@ -2,7 +2,7 @@ import { anything, anyFunction, instance, mock, verify, when } from 'ts-mockito' import querystring from 'node:querystring'; import WebSocket from 'isomorphic-ws'; -import { CallAwaiter } from '../test'; +import { CallAwaiter } from './test'; import { KubeConfig } from './config.js'; import { Exec } from './exec.js'; import { Cp } from './cp.js'; diff --git a/src/exec_test.ts b/src/exec_test.ts index 7cf73f8fc3..a5e7ab1b1f 100644 --- a/src/exec_test.ts +++ b/src/exec_test.ts @@ -3,7 +3,7 @@ import WebSocket from 'isomorphic-ws'; import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers'; import { anyFunction, anything, capture, instance, mock, verify, when } from 'ts-mockito'; -import { CallAwaiter, matchBuffer, ResizableWriteableStreamBuffer } from '../test'; +import { CallAwaiter, matchBuffer, ResizableWriteableStreamBuffer } from './test'; import { V1Status } from './api.js'; import { KubeConfig } from './config.js'; import { Exec } from './exec.js'; diff --git a/src/gcp_auth_test.ts b/src/gcp_auth_test.ts index ee79f0950f..3737530b28 100644 --- a/src/gcp_auth_test.ts +++ b/src/gcp_auth_test.ts @@ -282,7 +282,7 @@ describe('GoogleCloudPlatformAuth', () => { authProvider: { name: 'gcp', config: { - 'cmd-path': join(__dirname, '..', 'test', 'echo space.js'), + 'cmd-path': join(__dirname, 'test', 'echo space.js'), 'cmd-args': `'${responseStr}'`, 'token-key': '{.token.accessToken}', 'expiry-key': '{.token.token_expiry}', diff --git a/src/test/call-awaiter.ts b/src/test/call-awaiter.ts new file mode 100644 index 0000000000..81b59d8e0d --- /dev/null +++ b/src/test/call-awaiter.ts @@ -0,0 +1,13 @@ +import { EventEmitter } from 'node:events'; + +export class CallAwaiter extends EventEmitter { + public awaitCall(event: string) { + return new Promise((resolve) => { + this.once(event, resolve); + }); + } + + public resolveCall(event: string) { + return (...args: any[]) => this.emit(event, ...args); + } +} diff --git a/test/echo space.js b/src/test/echo space.js similarity index 53% rename from test/echo space.js rename to src/test/echo space.js index a36d8b7bdc..25e3131ef2 100755 --- a/test/echo space.js +++ b/src/test/echo space.js @@ -1,4 +1,4 @@ #!/usr/bin/env node // Just echo back all the args -console.log(process.argv.slice(2).join(' ')) \ No newline at end of file +console.log(process.argv.slice(2).join(' ')); diff --git a/test/index.ts b/src/test/index.ts similarity index 100% rename from test/index.ts rename to src/test/index.ts diff --git a/src/test/match-buffer.ts b/src/test/match-buffer.ts new file mode 100644 index 0000000000..a48bf8f1f7 --- /dev/null +++ b/src/test/match-buffer.ts @@ -0,0 +1,75 @@ +import { expect } from 'chai'; +import { RequestOptions, Agent } from 'node:https'; +import { Matcher } from 'ts-mockito/lib/matcher/type/Matcher'; + +export function matchBuffer(channel: number, contents: string): StringBufferMatcher { + return new StringBufferMatcher(channel, contents); +} + +class StringBufferMatcher extends Matcher { + constructor( + private channel: number, + private contents: string, + ) { + super(); + } + + public valueOf(): string { + return this.contents; + } + + public match(value: any): boolean { + if (value instanceof Buffer) { + const buffer = value as Buffer; + const channel: number = buffer.readInt8(0); + const contents: string = buffer.toString('utf8', 1); + return this.channel === channel && this.contents === contents; + } + + return false; + } + + public toString(): string { + return `buffer did not contain "${this.contents}"`; + } +} + +export function assertRequestAgentsEqual(agent1: Agent, agent2: Agent): void { + const BUFFER_EQUAL_TRUE = 0; + const ca1 = agent1.options.ca; + const ca2 = agent2.options.ca; + //@ts-ignore + if (ca1 !== ca2 && Buffer.compare(ca1, ca2) !== BUFFER_EQUAL_TRUE) { + throw 'unequal agent ca buffer'; + } + const cert1 = agent1.options.cert; + const cert2 = agent2.options.cert; + //@ts-ignore + if (cert1 !== cert2 && Buffer.compare(cert1, cert2) !== BUFFER_EQUAL_TRUE) { + throw 'unequal agent cert buffer'; + } + + const key1 = agent1.options.key; + const key2 = agent2.options.key; + //@ts-ignore + if (key1 !== key2 && Buffer.compare(key1, key2) !== BUFFER_EQUAL_TRUE) { + throw 'unequal agent key buffer'; + } + + expect(agent1.options.passphrase).to.equal(agent2.options.passphrase); + expect(agent1.options.pfx).to.equal(agent2.options.pfx); + expect(agent1.options.rejectUnauthorized).to.equal(agent2.options.rejectUnauthorized); +} + +export function assertRequestOptionsEqual(options1: RequestOptions, options2: RequestOptions): void { + //@ts-ignore agent has type Agent | Boolean which we expect to be populated with Agent here + const agent1: Agent = options1.agent; + //@ts-ignore + const agent2: Agent = options2.agent; + assertRequestAgentsEqual(agent1, agent2); + + expect(options1.auth).to.equal(options2.auth); + expect(options1.headers).to.deep.equal(options2.headers); + expect(options1.rejectUnauthorized).to.equal(options2.rejectUnauthorized); + expect(options1.servername).to.equal(options2.servername); +} diff --git a/test/resizable-writeable-stream-buffer.ts b/src/test/resizable-writeable-stream-buffer.ts similarity index 73% rename from test/resizable-writeable-stream-buffer.ts rename to src/test/resizable-writeable-stream-buffer.ts index e2a0a76ae3..248feed156 100644 --- a/test/resizable-writeable-stream-buffer.ts +++ b/src/test/resizable-writeable-stream-buffer.ts @@ -1,6 +1,6 @@ import { WritableStreamBuffer } from 'stream-buffers'; export class ResizableWriteableStreamBuffer extends WritableStreamBuffer implements NodeJS.WritableStream { - public columns: number = 0; - public rows: number = 0; + public columns: number = 0; + public rows: number = 0; } diff --git a/test/call-awaiter.ts b/test/call-awaiter.ts deleted file mode 100644 index dd165207f0..0000000000 --- a/test/call-awaiter.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { EventEmitter } from 'node:events'; - -export class CallAwaiter extends EventEmitter { - public awaitCall(event: string) { - return new Promise((resolve) => { - this.once(event, resolve); - }); - } - - public resolveCall(event: string) { - return (...args: any[]) => this.emit(event, ...args); - } -} diff --git a/test/match-buffer.ts b/test/match-buffer.ts deleted file mode 100644 index 0136ad9ea9..0000000000 --- a/test/match-buffer.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { expect } from 'chai'; -import { RequestOptions, Agent } from 'node:https'; -import { Matcher } from 'ts-mockito/lib/matcher/type/Matcher'; - -export function matchBuffer(channel: number, contents: string): StringBufferMatcher { - return new StringBufferMatcher(channel, contents); -} - -class StringBufferMatcher extends Matcher { - constructor(private channel: number, private contents: string) { - super(); - } - - public valueOf(): string { - return this.contents; - } - - public match(value: any): boolean { - if (value instanceof Buffer) { - const buffer = value as Buffer; - const channel: number = buffer.readInt8(0); - const contents: string = buffer.toString('utf8', 1); - return this.channel === channel && this.contents === contents; - } - - return false; - } - - public toString(): string { - return `buffer did not contain "${this.contents}"`; - } -} - -export function assertRequestAgentsEqual(agent1: Agent, agent2: Agent): void { - const BUFFER_EQUAL_TRUE = 0 - const ca1 = agent1.options.ca - const ca2 = agent2.options.ca - //@ts-ignore - if(ca1 !== ca2 && Buffer.compare(ca1,ca2) !== BUFFER_EQUAL_TRUE){ - throw("unequal agent ca buffer") - } - const cert1 = agent1.options.cert - const cert2 = agent2.options.cert - //@ts-ignore - if(cert1 !== cert2 && Buffer.compare(cert1,cert2) !== BUFFER_EQUAL_TRUE){ - throw("unequal agent cert buffer") - - } - - const key1 = agent1.options.key - const key2 = agent2.options.key - //@ts-ignore - if(key1 !== key2 && Buffer.compare(key1,key2) !== BUFFER_EQUAL_TRUE){ - throw("unequal agent key buffer") - } - - expect(agent1.options.passphrase).to.equal(agent2.options.passphrase); - expect(agent1.options.pfx).to.equal(agent2.options.pfx); - expect(agent1.options.rejectUnauthorized).to.equal(agent2.options.rejectUnauthorized); -} - -export function assertRequestOptionsEqual(options1: RequestOptions, options2: RequestOptions): void { - //@ts-ignore agent has type Agent | Boolean which we expect to be populated with Agent here - const agent1: Agent = options1.agent - //@ts-ignore - const agent2: Agent = options2.agent - assertRequestAgentsEqual(agent1, agent2) - - expect(options1.auth).to.equal(options2.auth) - expect(options1.headers).to.deep.equal(options2.headers) - expect(options1.rejectUnauthorized).to.equal(options2.rejectUnauthorized) - expect(options1.servername).to.equal(options2.servername) -} diff --git a/tsconfig.json b/tsconfig.json index 5d5b1874bc..33c6dc5cf1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,6 +19,6 @@ // enable this when it works with tslint, or we switch to prettier // "declarationMap": true }, - "exclude": ["node_modules", "src/*_test.ts", "dist"], + "exclude": ["node_modules", "src/*_test.ts", "src/test", "dist"], "include": ["*.ts", "src/**/*"] }