Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2import { commandEnv, createOutputTail } from '../src/command'
3
4describe('commandEnv', () => {
5 afterEach(() => {
6 vi.unstubAllEnvs()
7 })
8
9 it('gives a command only the listed variables of this process, plus env', () => {
10 vi.stubEnv('SECRET_TOKEN', 'hunter2')
11 vi.stubEnv('LANG', 'C.UTF-8')
12 vi.stubEnv('SystemRoot', 'C:\\Windows')
13 vi.stubEnv('HTTPS_PROXY', 'http://proxy:8080')
14
15 const linux = commandEnv({ AGENT: '1' }, 'linux')
16 expect(linux).toMatchObject({ LANG: 'C.UTF-8', AGENT: '1' })
17 expect(linux).not.toHaveProperty('SECRET_TOKEN')
18 // The Windows variables and setting are for Windows only.
19 expect(linux).not.toHaveProperty('SystemRoot')
20 expect(linux).not.toHaveProperty('NoDefaultCurrentDirectoryInExePath')
21 expect(linux).not.toHaveProperty('HTTPS_PROXY')
22
23 expect(commandEnv(undefined, 'linux', ['HTTPS_PROXY'])).toMatchObject({
24 HTTPS_PROXY: 'http://proxy:8080',
25 })
26
27 const windows = commandEnv(
28 { AGENT: '1', NoDefaultCurrentDirectoryInExePath: '' },
29 'win32',
30 )
31 expect(windows).toMatchObject({
32 SystemRoot: 'C:\\Windows',
33 AGENT: '1',
34 NoDefaultCurrentDirectoryInExePath: '1',
35 })
36 expect(windows).not.toHaveProperty('SECRET_TOKEN')
37 })
38})
39
40describe('createOutputTail', () => {
41 it('gives all output under the limit, with no note', () => {
42 const output = createOutputTail(10)
43 output.push('ab')
44 output.push(new TextEncoder().encode('cd'))
45 expect(output.text()).toBe('abcd')
46 })
47
48 it('keeps the newest bytes over the limit, and tells how many were dropped', () => {
49 const output = createOutputTail(4)
50 output.push('abc')
51 output.push('def')
52 expect(output.text()).toBe('[2 earlier bytes were dropped.]\ncdef')
53 })
54
55 it('keeps the end of one chunk that is bigger than the limit', () => {
56 const output = createOutputTail(3)
57 output.push('abcdef')
58 expect(output.text()).toBe('[3 earlier bytes were dropped.]\ndef')
59 })
60
61 it('starts at a whole character when the cut is in one', () => {
62 const output = createOutputTail(4)
63 // `é` is 2 bytes. The cut drops its first byte, so it drops all of it.
64 output.push('é')
65 output.push('abc')
66 expect(output.text()).toBe('[2 earlier bytes were dropped.]\nabc')
67 })
68})