|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { writeSentryOptionsEnvironment } from '../../plugin/src/utils'; |
| 5 | + |
| 6 | +jest.mock('../../plugin/src/logger'); |
| 7 | + |
| 8 | +describe('writeSentryOptionsEnvironment', () => { |
| 9 | + let tempDir: string; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sentry-options-test-')); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 17 | + }); |
| 18 | + |
| 19 | + test('creates sentry.options.json with environment when file does not exist', () => { |
| 20 | + writeSentryOptionsEnvironment(tempDir, 'staging'); |
| 21 | + |
| 22 | + const filePath = path.join(tempDir, 'sentry.options.json'); |
| 23 | + const content = JSON.parse(fs.readFileSync(filePath, 'utf8')); |
| 24 | + expect(content).toEqual({ environment: 'staging' }); |
| 25 | + }); |
| 26 | + |
| 27 | + test('sets environment in existing sentry.options.json', () => { |
| 28 | + const filePath = path.join(tempDir, 'sentry.options.json'); |
| 29 | + fs.writeFileSync(filePath, JSON.stringify({ dsn: 'https://key@sentry.io/123', environment: 'production' })); |
| 30 | + |
| 31 | + writeSentryOptionsEnvironment(tempDir, 'staging'); |
| 32 | + |
| 33 | + const content = JSON.parse(fs.readFileSync(filePath, 'utf8')); |
| 34 | + expect(content.environment).toBe('staging'); |
| 35 | + expect(content.dsn).toBe('https://key@sentry.io/123'); |
| 36 | + }); |
| 37 | + |
| 38 | + test('adds environment to existing sentry.options.json without environment', () => { |
| 39 | + const filePath = path.join(tempDir, 'sentry.options.json'); |
| 40 | + fs.writeFileSync(filePath, JSON.stringify({ dsn: 'https://key@sentry.io/123' })); |
| 41 | + |
| 42 | + writeSentryOptionsEnvironment(tempDir, 'staging'); |
| 43 | + |
| 44 | + const content = JSON.parse(fs.readFileSync(filePath, 'utf8')); |
| 45 | + expect(content.environment).toBe('staging'); |
| 46 | + expect(content.dsn).toBe('https://key@sentry.io/123'); |
| 47 | + }); |
| 48 | + |
| 49 | + test('does not crash and warns when sentry.options.json contains invalid JSON', () => { |
| 50 | + const { warnOnce } = require('../../plugin/src/logger'); |
| 51 | + const filePath = path.join(tempDir, 'sentry.options.json'); |
| 52 | + fs.writeFileSync(filePath, 'invalid json{{{'); |
| 53 | + |
| 54 | + writeSentryOptionsEnvironment(tempDir, 'staging'); |
| 55 | + |
| 56 | + expect(warnOnce).toHaveBeenCalledWith(expect.stringContaining('Failed to parse')); |
| 57 | + // File should remain unchanged |
| 58 | + expect(fs.readFileSync(filePath, 'utf8')).toBe('invalid json{{{'); |
| 59 | + }); |
| 60 | +}); |
0 commit comments