50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
|
|
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||
|
|
import * as fs from 'node:fs/promises';
|
||
|
|
import * as os from 'node:os';
|
||
|
|
import * as path from 'node:path';
|
||
|
|
|
||
|
|
import { SshConfig } from '../ts/index.js';
|
||
|
|
|
||
|
|
tap.test('should read main ssh config and included hosts', async () => {
|
||
|
|
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'dap-sshconfig-'));
|
||
|
|
try {
|
||
|
|
const sshDir = path.join(tempDir, '.ssh');
|
||
|
|
const includeDir = path.join(sshDir, 'config.d');
|
||
|
|
await fs.mkdir(includeDir, { recursive: true });
|
||
|
|
const mainConfigPath = path.join(sshDir, 'config');
|
||
|
|
await fs.writeFile(
|
||
|
|
mainConfigPath,
|
||
|
|
[
|
||
|
|
'Include config.d/*',
|
||
|
|
'',
|
||
|
|
'Host alpha',
|
||
|
|
' HostName alpha.example.com',
|
||
|
|
' User deploy',
|
||
|
|
'',
|
||
|
|
'# dap:begin beta',
|
||
|
|
'Host beta',
|
||
|
|
' HostName beta.example.com',
|
||
|
|
'# dap:end beta',
|
||
|
|
'',
|
||
|
|
].join('\n')
|
||
|
|
);
|
||
|
|
await fs.writeFile(path.join(includeDir, 'hosts'), ['Host gamma', ' HostName gamma.example.com'].join('\n'));
|
||
|
|
|
||
|
|
const sshConfig = new SshConfig({ homeDir: tempDir, mainConfigPath });
|
||
|
|
const result = await sshConfig.read();
|
||
|
|
const aliases = result.hosts.map((host) => host.patterns[0]);
|
||
|
|
|
||
|
|
expect(aliases.includes('alpha')).toEqual(true);
|
||
|
|
expect(aliases.includes('beta')).toEqual(true);
|
||
|
|
expect(aliases.includes('gamma')).toEqual(true);
|
||
|
|
expect(result.hosts.find((host) => host.patterns[0] === 'beta')?.dapManaged).toEqual(true);
|
||
|
|
expect(result.hosts.find((host) => host.patterns[0] === 'alpha')?.options.hostname?.[0]).toEqual(
|
||
|
|
'alpha.example.com'
|
||
|
|
);
|
||
|
|
} finally {
|
||
|
|
await fs.rm(tempDir, { recursive: true, force: true });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
export default tap.start();
|