|
| 1 | +// @ts-expect-error - clack is ESM and TS complains about that. It works though |
| 2 | +import * as clack from '@clack/prompts'; |
| 3 | +import * as fs from 'node:fs'; |
| 4 | +import * as os from 'node:os'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 7 | +import { configureSentryCLI } from '../../src/apple/configure-sentry-cli'; |
| 8 | + |
| 9 | +vi.mock('@clack/prompts', async () => ({ |
| 10 | + __esModule: true, |
| 11 | + default: await vi.importActual<typeof clack>('@clack/prompts'), |
| 12 | +})); |
| 13 | + |
| 14 | +describe('configureSentryCLI', () => { |
| 15 | + const authToken = 'test'; |
| 16 | + let projectDir: string; |
| 17 | + let rcPath: string; |
| 18 | + let gitignorePath: string; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + beforeEach(() => { |
| 22 | + vi.spyOn(clack.log, 'warn').mockImplementation(() => { |
| 23 | + /* empty */ |
| 24 | + }); |
| 25 | + vi.spyOn(clack, 'select').mockResolvedValue(undefined); |
| 26 | + }); |
| 27 | + |
| 28 | + projectDir = fs.mkdtempSync(path.join(os.tmpdir(), 'project')); |
| 29 | + fs.mkdirSync(projectDir, { recursive: true }); |
| 30 | + |
| 31 | + rcPath = path.join(projectDir, '.sentryclirc'); |
| 32 | + gitignorePath = path.join(projectDir, '.gitignore'); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('.sentryclirc file not exists', () => { |
| 36 | + it('should create the .sentryclirc file', () => { |
| 37 | + // -- Arrange -- |
| 38 | + // Pre-condition is that the .sentryclirc file does not exist |
| 39 | + expect(fs.existsSync(rcPath)).toBe(false); |
| 40 | + |
| 41 | + // -- Act -- |
| 42 | + configureSentryCLI({ projectDir, authToken }); |
| 43 | + |
| 44 | + // -- Assert -- |
| 45 | + expect(fs.existsSync(rcPath)).toBe(true); |
| 46 | + expect(fs.readFileSync(rcPath, 'utf8')).toContain(`token=test`); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('.sentryclirc file exists', () => { |
| 51 | + it('should update the .sentryclirc file', () => { |
| 52 | + // -- Arrange -- |
| 53 | + // Pre-condition is that the .sentryclirc file exists |
| 54 | + fs.writeFileSync(rcPath, `token=old`); |
| 55 | + |
| 56 | + // -- Act -- |
| 57 | + configureSentryCLI({ projectDir, authToken }); |
| 58 | + |
| 59 | + // -- Assert -- |
| 60 | + expect(fs.readFileSync(rcPath, 'utf8')).toContain(`token=${authToken}`); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('.gitignore file not exists', () => { |
| 65 | + it('should create the .gitignore file', () => { |
| 66 | + // -- Arrange -- |
| 67 | + // Pre-condition is that the .gitignore file does not exist |
| 68 | + expect(fs.existsSync(gitignorePath)).toBe(false); |
| 69 | + |
| 70 | + // -- Act -- |
| 71 | + configureSentryCLI({ projectDir, authToken }); |
| 72 | + |
| 73 | + // -- Assert -- |
| 74 | + expect(fs.existsSync(gitignorePath)).toBe(true); |
| 75 | + expect(fs.readFileSync(gitignorePath, 'utf8')).toContain('.sentryclirc'); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('.gitignore file exists', () => { |
| 80 | + describe("contains '.sentryclirc'", () => { |
| 81 | + it('should not append the .sentryclirc file to the .gitignore file', () => { |
| 82 | + // -- Arrange -- |
| 83 | + // Pre-condition is that the .gitignore file exists and contains '.sentryclirc' |
| 84 | + fs.writeFileSync( |
| 85 | + gitignorePath, |
| 86 | + ` |
| 87 | +# Xcode |
| 88 | +xcuserdata/ |
| 89 | +
|
| 90 | +# Sentry |
| 91 | +.sentryclirc |
| 92 | +`, |
| 93 | + ); |
| 94 | + |
| 95 | + // -- Act -- |
| 96 | + configureSentryCLI({ projectDir, authToken }); |
| 97 | + |
| 98 | + // -- Assert -- |
| 99 | + expect(fs.readFileSync(gitignorePath, 'utf8')).toContain( |
| 100 | + '.sentryclirc', |
| 101 | + ); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe("does not contain '.sentryclirc'", () => { |
| 106 | + it('should append the .sentryclirc file to the .gitignore file', () => { |
| 107 | + // -- Arrange -- |
| 108 | + // Pre-condition is that the .gitignore file exists and does not contain '.sentryclirc' |
| 109 | + fs.writeFileSync( |
| 110 | + gitignorePath, |
| 111 | + ` |
| 112 | +# Xcode |
| 113 | +xcuserdata/ |
| 114 | +`, |
| 115 | + ); |
| 116 | + |
| 117 | + // -- Act -- |
| 118 | + configureSentryCLI({ projectDir, authToken }); |
| 119 | + |
| 120 | + // -- Assert -- |
| 121 | + expect(fs.readFileSync(gitignorePath, 'utf8')).toBe( |
| 122 | + ` |
| 123 | +# Xcode |
| 124 | +xcuserdata/ |
| 125 | +
|
| 126 | +.sentryclirc |
| 127 | +`, |
| 128 | + ); |
| 129 | + }); |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments