@cloud-cli/dns 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import fs from 'fs';
2
- import { join } from 'path';
1
+ import fs from 'node:fs';
2
+ import { join } from 'node:path';
3
3
  import { exec } from '@cloud-cli/exec';
4
4
  import { init } from '@cloud-cli/cli';
5
5
  const filePath = join(process.cwd(), 'configuration', 'hosts.conf');
@@ -1,10 +1,15 @@
1
1
  import dns, { parseDNSLine } from './index';
2
2
  import fs from 'fs';
3
- import * as exec from '@cloud-cli/exec';
4
3
  import { init } from '@cloud-cli/cli';
4
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
5
+ const execMocks = vi.hoisted(() => ({
6
+ exec: vi.fn(),
7
+ }));
8
+ vi.mock('get-port', () => ({ default: vi.fn().mockReturnValue(1234) }));
9
+ vi.mock('@cloud-cli/exec', () => ({ exec: execMocks.exec }));
5
10
  beforeEach(() => {
6
- jest.spyOn(fs, 'writeFileSync').mockImplementation();
7
- jest.spyOn(fs, 'readFileSync').mockImplementation(() => '');
11
+ vi.spyOn(fs, 'writeFileSync').mockImplementation(() => { });
12
+ vi.spyOn(fs, 'readFileSync').mockImplementation(() => '');
8
13
  });
9
14
  describe('dns', () => {
10
15
  it('should parse a DNS configuration line', () => {
@@ -18,8 +23,8 @@ describe('dns', () => {
18
23
  it('should load entries from file', () => {
19
24
  let fileExists = false;
20
25
  const buffer = `1.2.3.4 test\n5.6.7.8 foo`;
21
- jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
22
- jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
26
+ vi.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
27
+ vi.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
23
28
  expect(dns.list()).toEqual([]);
24
29
  fileExists = true;
25
30
  const list = dns.list();
@@ -31,16 +36,18 @@ describe('dns', () => {
31
36
  it('should get a DNS entry by domain', () => {
32
37
  let fileExists = false;
33
38
  const buffer = `1.2.3.4 test.com\n5.6.7.8 foo.com`;
34
- jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
35
- jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
39
+ vi.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
40
+ vi.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
36
41
  expect(dns.get({ domain: 'foo.com' })).toBe(null);
37
42
  fileExists = true;
38
43
  expect(dns.get({ domain: 'foo.com' })).toEqual({ domain: 'foo.com', target: '5.6.7.8' });
39
44
  });
40
45
  it('should add and remove local DNS entries', () => {
41
46
  let text = '';
42
- jest.spyOn(fs, 'writeFileSync').mockImplementation((_file, value) => { text = value; });
43
- jest.spyOn(fs, 'readFileSync').mockImplementation(() => text);
47
+ vi.spyOn(fs, 'writeFileSync').mockImplementation((_file, value) => {
48
+ text = value;
49
+ });
50
+ vi.spyOn(fs, 'readFileSync').mockImplementation(() => text);
44
51
  dns.add({ domain: 'foo', target: '2.3.4.5' });
45
52
  dns.add({ domain: 'bar', target: '2.3.4.5' });
46
53
  dns.add({ domain: 'baz' });
@@ -53,15 +60,15 @@ describe('dns', () => {
53
60
  });
54
61
  describe('reload', () => {
55
62
  it('should reload the DNS service', async () => {
56
- jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: true, stdout: '123' });
57
- jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: true });
63
+ execMocks.exec.mockResolvedValueOnce({ ok: true, stdout: '123' });
64
+ execMocks.exec.mockResolvedValueOnce({ ok: true });
58
65
  await expect(dns.reload()).resolves.toBe(true);
59
- expect(exec.exec).toHaveBeenCalledWith('pidof', ['dnsmasq']);
60
- expect(exec.exec).toHaveBeenCalledWith('kill', ['-s', 'HUP', '123']);
66
+ expect(execMocks.exec).toHaveBeenCalledWith('pidof', ['dnsmasq']);
67
+ expect(execMocks.exec).toHaveBeenCalledWith('kill', ['-s', 'HUP', '123']);
61
68
  });
62
69
  it('should show error on reload', async () => {
63
- jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: true, stdout: '123' });
64
- jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: false, stderr: 'error' });
70
+ execMocks.exec.mockResolvedValueOnce({ ok: true, stdout: '123' });
71
+ execMocks.exec.mockResolvedValueOnce({ ok: false, stderr: 'error' });
65
72
  await expect(dns.reload()).rejects.toEqual(new Error('Failed to reload'));
66
73
  });
67
74
  });
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "@cloud-cli/dns",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
- "test": "jest",
10
- "tdd": "jest --watchAll"
9
+ "test": "vitest"
11
10
  },
12
11
  "prettier": "@cloud-cli/prettier-config",
13
12
  "repository": {
@@ -18,12 +17,16 @@
18
17
  "access": "public"
19
18
  },
20
19
  "devDependencies": {
21
- "@cloud-cli/jest-config": "^1.0.0",
22
20
  "@cloud-cli/prettier-config": "^1.0.0",
23
- "@cloud-cli/typescript-config": "^1.0.0"
21
+ "@cloud-cli/typescript-config": "^1.0.0",
22
+ "@types/node": "^20.12.8",
23
+ "typescript": "^5.4.5",
24
+ "vitest": "^1.6.0"
24
25
  },
25
26
  "dependencies": {
26
- "@cloud-cli/cli": "^1.6.0",
27
27
  "@cloud-cli/exec": "^1.0.4"
28
+ },
29
+ "peerDependencies": {
30
+ "@cloud-cli/cli": "^2.0.0"
28
31
  }
29
32
  }
package/src/index.spec.ts CHANGED
@@ -1,11 +1,18 @@
1
1
  import dns, { parseDNSLine } from './index';
2
2
  import fs from 'fs';
3
- import * as exec from '@cloud-cli/exec';
4
3
  import { init } from '@cloud-cli/cli';
4
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
5
+
6
+ const execMocks = vi.hoisted(() => ({
7
+ exec: vi.fn(),
8
+ }));
9
+
10
+ vi.mock('get-port', () => ({ default: vi.fn().mockReturnValue(1234) }));
11
+ vi.mock('@cloud-cli/exec', () => ({ exec: execMocks.exec }));
5
12
 
6
13
  beforeEach(() => {
7
- jest.spyOn(fs, 'writeFileSync').mockImplementation();
8
- jest.spyOn(fs, 'readFileSync').mockImplementation(() => '');
14
+ vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {});
15
+ vi.spyOn(fs, 'readFileSync').mockImplementation(() => '');
9
16
  });
10
17
 
11
18
  describe('dns', () => {
@@ -22,8 +29,8 @@ describe('dns', () => {
22
29
  it('should load entries from file', () => {
23
30
  let fileExists = false;
24
31
  const buffer = `1.2.3.4 test\n5.6.7.8 foo`;
25
- jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
26
- jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
32
+ vi.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
33
+ vi.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
27
34
 
28
35
  expect(dns.list()).toEqual([]);
29
36
 
@@ -32,14 +39,14 @@ describe('dns', () => {
32
39
  expect(list).toEqual([
33
40
  { domain: 'test', target: '1.2.3.4' },
34
41
  { domain: 'foo', target: '5.6.7.8' },
35
- ])
42
+ ]);
36
43
  });
37
44
 
38
45
  it('should get a DNS entry by domain', () => {
39
46
  let fileExists = false;
40
47
  const buffer = `1.2.3.4 test.com\n5.6.7.8 foo.com`;
41
- jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
42
- jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
48
+ vi.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
49
+ vi.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
43
50
 
44
51
  expect(dns.get({ domain: 'foo.com' })).toBe(null);
45
52
 
@@ -49,8 +56,10 @@ describe('dns', () => {
49
56
 
50
57
  it('should add and remove local DNS entries', () => {
51
58
  let text = '';
52
- jest.spyOn(fs, 'writeFileSync').mockImplementation((_file, value: string) => { text = value; });
53
- jest.spyOn(fs, 'readFileSync').mockImplementation(() => text);
59
+ vi.spyOn(fs, 'writeFileSync').mockImplementation((_file, value: string) => {
60
+ text = value;
61
+ });
62
+ vi.spyOn(fs, 'readFileSync').mockImplementation(() => text);
54
63
 
55
64
  dns.add({ domain: 'foo', target: '2.3.4.5' });
56
65
  dns.add({ domain: 'bar', target: '2.3.4.5' });
@@ -68,17 +77,17 @@ describe('dns', () => {
68
77
 
69
78
  describe('reload', () => {
70
79
  it('should reload the DNS service', async () => {
71
- jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: true, stdout: '123' } as any);
72
- jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: true } as any);
80
+ execMocks.exec.mockResolvedValueOnce({ ok: true, stdout: '123' } as any);
81
+ execMocks.exec.mockResolvedValueOnce({ ok: true } as any);
73
82
 
74
83
  await expect(dns.reload()).resolves.toBe(true);
75
- expect(exec.exec).toHaveBeenCalledWith('pidof', ['dnsmasq']);
76
- expect(exec.exec).toHaveBeenCalledWith('kill', ['-s', 'HUP', '123']);
84
+ expect(execMocks.exec).toHaveBeenCalledWith('pidof', ['dnsmasq']);
85
+ expect(execMocks.exec).toHaveBeenCalledWith('kill', ['-s', 'HUP', '123']);
77
86
  });
78
87
 
79
88
  it('should show error on reload', async () => {
80
- jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: true, stdout: '123' } as any);
81
- jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: false, stderr: 'error' } as any);
89
+ execMocks.exec.mockResolvedValueOnce({ ok: true, stdout: '123' } as any);
90
+ execMocks.exec.mockResolvedValueOnce({ ok: false, stderr: 'error' } as any);
82
91
 
83
92
  await expect(dns.reload()).rejects.toEqual(new Error('Failed to reload'));
84
93
  });
@@ -91,5 +100,5 @@ describe('dns', () => {
91
100
 
92
101
  expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '1.1.2.2 bar');
93
102
  });
94
- })
103
+ });
95
104
  });
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
- import fs from 'fs';
2
- import { join } from 'path';
1
+ import fs from 'node:fs';
2
+ import { join } from 'node:path';
3
3
  import { exec } from '@cloud-cli/exec';
4
4
  import { init } from '@cloud-cli/cli';
5
5