UNPKG

@sentry/wizard

Version:

Sentry wizard helping you to configure your project

482 lines (472 loc) 17.9 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs = __importStar(require("node:fs")); const os = __importStar(require("node:os")); const path = __importStar(require("node:path")); const fastlane_1 = require("../../src/apple/fastlane"); // @ts-expect-error - clack is ESM and TS complains about that. It works though const clack = __importStar(require("@clack/prompts")); const vitest_1 = require("vitest"); vitest_1.vi.mock('@clack/prompts', async () => ({ __esModule: true, ...(await vitest_1.vi.importActual('@clack/prompts')), })); (0, vitest_1.describe)('fastlane', () => { (0, vitest_1.beforeEach)(() => { vitest_1.vi.spyOn(clack.log, 'warn').mockImplementation(() => { /* empty */ }); vitest_1.vi.spyOn(clack, 'select').mockResolvedValue(undefined); }); (0, vitest_1.describe)('#fastFile', () => { (0, vitest_1.describe)('file exists', () => { (0, vitest_1.it)('should return path', () => { // -- Arrange -- const { fastlaneDir, projectPath } = createFastlaneDir(); const fastfile = createFastfile(fastlaneDir, 'lane :test do'); // -- Act -- const result = (0, fastlane_1.fastFile)(projectPath); // -- Assert -- (0, vitest_1.expect)(result).toBe(fastfile); }); }); (0, vitest_1.describe)('file does not exist', () => { (0, vitest_1.it)('should return null', () => { // -- Arrange -- const { projectPath } = createFastlaneDir(); // do not create Fastfile // -- Act -- const result = (0, fastlane_1.fastFile)(projectPath); // -- Assert -- (0, vitest_1.expect)(result).toBeNull(); }); }); }); (0, vitest_1.describe)('#findIOSPlatform', () => { (0, vitest_1.describe)('platform block detection', () => { const variations = [ { name: 'no platform', content: `lane :test do end`, expected: { index: 0, length: 17 }, }, { name: 'platform is ios', content: `platform: ios lane :test do end`, expected: { index: 0, length: 31 }, }, { name: 'platform is ios and other platform', content: `platform: ios end platform: android lane :test do end`, expected: { index: 0, length: 57 }, }, { name: 'platform is ios and other platform', content: ` \t\tplatform: ios \t\tend \t\tplatform: android \t\t\tlane :test do \t\t\tend`, expected: { index: 0, length: 66 }, }, ]; for (const variation of variations) { (0, vitest_1.describe)(`${variation.name}`, () => { (0, vitest_1.it)('should return null', () => { // -- Act -- const result = fastlane_1.exportForTesting.findIOSPlatform(variation.content); // -- Assert -- (0, vitest_1.expect)(result).toEqual(variation.expected); }); }); } }); (0, vitest_1.describe)('platform block not found', () => { (0, vitest_1.it)('should return full content', () => { // -- Arrange -- const content = ` lane :test do puts 'Hello, world!' end`; // -- Act -- const result = fastlane_1.exportForTesting.findIOSPlatform(content); // -- Assert -- (0, vitest_1.expect)(result).toEqual({ index: 0, length: 65 }); }); }); (0, vitest_1.describe)('invalid platform block', () => { (0, vitest_1.it)('should return null', () => { // -- Arrange -- // platform block is not opened with `do` const content = ` platform :ios\n lane :test do puts 'Hello, world!' end end `; // -- Act -- const result = fastlane_1.exportForTesting.findIOSPlatform(content); // -- Assert -- (0, vitest_1.expect)(result).toBeNull(); }); }); (0, vitest_1.describe)('platform block is not closed', () => { (0, vitest_1.it)('should return null', () => { // -- Arrange -- const content = ` platform :ios do lane :test do puts 'Hello, world!' end `; // -- Act -- const result = fastlane_1.exportForTesting.findIOSPlatform(content); // -- Assert -- (0, vitest_1.expect)(result).toBeNull(); }); }); (0, vitest_1.describe)('multiple platforms detected', () => { (0, vitest_1.it)('should return block with ios platform', () => { // -- Arrange -- const content = ` fastlane_version '2.53.1' before_all do ensure_git_branch ensure_git_status_clean git_pull end platform :ios do # iOS Lanes end platform :android do # Android Lanes end `; // -- Act -- const result = fastlane_1.exportForTesting.findIOSPlatform(content); // -- Assert -- (0, vitest_1.expect)(result).toEqual({ index: 121, length: 15 }); }); }); }); (0, vitest_1.describe)('#findLanes', () => { (0, vitest_1.describe)('lanes detection', () => { (0, vitest_1.describe)('valid cases', () => { const variations = [ { name: 'single lane', content: ` lane :test do puts 'Hello, world!' end `, expected: [{ index: 17, length: 25, name: 'test' }], }, { name: 'multiple lanes', content: ` lane :test do puts 'Hello, world!' end lane :test2 do puts 'Hello, world!' end`, expected: [ { index: 17, length: 25, name: 'test' }, { index: 65, length: 25, name: 'test2' }, ], }, ]; for (const variation of variations) { (0, vitest_1.describe)(`${variation.name}`, () => { (0, vitest_1.it)('should return lanes', () => { // -- Act -- const result = fastlane_1.exportForTesting.findLanes(variation.content); // -- Assert -- (0, vitest_1.expect)(result).toEqual(variation.expected); }); }); } }); (0, vitest_1.describe)('invalid cases', () => { (0, vitest_1.describe)('lane is not indented', () => { (0, vitest_1.it)('should return null', () => { // -- Arrange -- const content = `lane :test do\nend`; // -- Act -- const result = fastlane_1.exportForTesting.findLanes(content); // -- Assert -- (0, vitest_1.expect)(result).toBeNull(); }); }); (0, vitest_1.describe)('lane is not closed', () => { (0, vitest_1.it)('should return null', () => { // -- Arrange -- const content = ` lane :test do\n`; // -- Act -- const result = fastlane_1.exportForTesting.findLanes(content); // -- Assert -- (0, vitest_1.expect)(result).toBeNull(); }); }); }); }); }); (0, vitest_1.describe)('#addSentryToLane', () => { (0, vitest_1.describe)('sentry_cli is not present', () => { (0, vitest_1.it)('should return original content', () => { // -- Arrange -- const content = ` platform :ios do lane :test do puts 'Hello, world!' end end `; const lane = { index: 34, length: 25, name: 'test' }; // -- Act -- const result = fastlane_1.exportForTesting.addSentryToLane(content, lane, 'test-org', 'test-project'); // -- Assert -- (0, vitest_1.expect)(result).toBe(` platform :ios do lane :test do puts 'Hello, world!' sentry_cli( org_slug: 'test-org', project_slug: 'test-project', include_sources: true ) end end `); }); }); (0, vitest_1.describe)('sentry_cli is present', () => { (0, vitest_1.it)('should return updated content', () => { // -- Arrange -- const content = ` platform :ios do lane :test do puts 'Hello, world!' sentry_cli(org_slug: 'test-org', project_slug: 'test-project') end end `; const lane = { index: 34, length: 92, name: 'test' }; // -- Act -- const result = fastlane_1.exportForTesting.addSentryToLane(content, lane, 'test-org', 'test-project'); // -- Assert -- (0, vitest_1.expect)(result).toBe(` platform :ios do lane :test do puts 'Hello, world!' sentry_cli( org_slug: 'test-org', project_slug: 'test-project', include_sources: true ) end end `); }); }); }); (0, vitest_1.describe)('#addSentryToFastlane', () => { const org = 'test-org'; const project = 'test-project'; (0, vitest_1.describe)('Fastfile not found', () => { (0, vitest_1.it)('should return false', async () => { // -- Arrange -- const { projectPath } = createFastlaneDir(); const fastfilePath = path.join(projectPath, 'Fastfile'); // do not create Fastfile // -- Act -- const result = await (0, fastlane_1.addSentryToFastlane)(projectPath, org, project); // -- Assert -- (0, vitest_1.expect)(result).toBe(false); (0, vitest_1.expect)(fs.existsSync(fastfilePath)).toBe(false); }); }); (0, vitest_1.describe)('platform not found', () => { (0, vitest_1.it)('should return false', async () => { // -- Arrange -- const { fastlaneDir, projectPath } = createFastlaneDir(); const fastfilePath = createFastfile(fastlaneDir, ` platform :ios lane :test do puts 'Hello, world!' end end `); const originalContent = fs.readFileSync(fastfilePath, 'utf8'); // -- Act -- const result = await (0, fastlane_1.addSentryToFastlane)(projectPath, org, project); // -- Assert -- (0, vitest_1.expect)(result).toBe(false); (0, vitest_1.expect)(fs.readFileSync(fastfilePath, 'utf8')).toBe(originalContent); }); }); (0, vitest_1.describe)('no lanes', () => { (0, vitest_1.it)('should return false', async () => { // -- Arrange -- const { fastlaneDir, projectPath } = createFastlaneDir(); createFastfile(fastlaneDir, `platform :ios`); // -- Act -- const result = await (0, fastlane_1.addSentryToFastlane)(projectPath, org, project); // -- Assert -- (0, vitest_1.expect)(result).toBe(false); }); (0, vitest_1.it)('should warn user', async () => { // -- Arrange -- const { fastlaneDir, projectPath } = createFastlaneDir(); createFastfile(fastlaneDir, `platform :ios`); // -- Act -- const result = await (0, fastlane_1.addSentryToFastlane)(projectPath, org, project); // -- Assert -- (0, vitest_1.expect)(result).toBe(false); (0, vitest_1.expect)(clack.log.warn).toHaveBeenCalledWith('No suitable lanes in your Fastfile.'); }); }); (0, vitest_1.describe)('single lane', () => { (0, vitest_1.it)('should return true', async () => { // -- Arrange -- const { fastlaneDir, projectPath } = createFastlaneDir(); const fastfilePath = createFastfile(fastlaneDir, ` platform :ios do lane :test do puts 'Hello, world!' end end `); // -- Act -- const result = await (0, fastlane_1.addSentryToFastlane)(projectPath, org, project); // -- Assert -- (0, vitest_1.expect)(result).toBe(true); (0, vitest_1.expect)(fs.readFileSync(fastfilePath, 'utf8')).toBe(` platform :ios do lane :test do puts 'Hello, world!' sentry_cli( org_slug: 'test-org', project_slug: 'test-project', include_sources: true ) end end `); }); }); (0, vitest_1.describe)('multiple lanes', () => { let fastfilePath; let projectPath; let originalContent; (0, vitest_1.beforeEach)(() => { const createdFastlaneDir = createFastlaneDir(); projectPath = createdFastlaneDir.projectPath; fastfilePath = createFastfile(createdFastlaneDir.fastlaneDir, `platform :ios do lane :test do puts 'Hello, world!' end lane :beta do puts 'Beta lane' end end `); }); (0, vitest_1.describe)('no lane selected', () => { (0, vitest_1.it)('should not modify Fastfile', async () => { // -- Arrange -- originalContent = fs.readFileSync(fastfilePath, 'utf8'); vitest_1.vi.spyOn(clack, 'select').mockResolvedValue(undefined); // -- Act -- const result = await (0, fastlane_1.addSentryToFastlane)(projectPath, org, project); // -- Assert -- (0, vitest_1.expect)(result).toBe(false); (0, vitest_1.expect)(fs.readFileSync(fastfilePath, 'utf8')).toBe(originalContent); }); }); (0, vitest_1.describe)('lane selected', () => { (0, vitest_1.it)('should modify only selected lane', async () => { // -- Arrange -- vitest_1.vi.spyOn(clack, 'select').mockResolvedValue({ value: 'beta', index: 1, }); // -- Act -- const result = await (0, fastlane_1.addSentryToFastlane)(projectPath, org, project); // -- Assert -- (0, vitest_1.expect)(result).toBe(true); (0, vitest_1.expect)(fs.readFileSync(fastfilePath, 'utf8')).toBe(`platform :ios do lane :test do puts 'Hello, world!' end lane :beta do puts 'Beta lane' sentry_cli( org_slug: 'test-org', project_slug: 'test-project', include_sources: true ) end end `); (0, vitest_1.expect)(clack.select).toHaveBeenCalledWith({ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment maxItems: vitest_1.expect.any(Number), message: 'Select lane to add Sentry to:', options: [ { value: { value: 'test', index: 0 }, label: 'test' }, { value: { value: 'beta', index: 1 }, label: 'beta' }, ], }); }); }); }); }); }); function createFastlaneDir() { const projectPath = fs.mkdtempSync(path.join(os.tmpdir(), 'test-project')); const fastlaneDir = path.join(projectPath, 'fastlane'); fs.mkdirSync(fastlaneDir, { recursive: true, }); return { fastlaneDir, projectPath }; } function createFastfile(fastlaneDir, content) { const fastfile = path.join(fastlaneDir, 'Fastfile'); fs.writeFileSync(fastfile, content); return fastfile; } //# sourceMappingURL=fastfile.test.js.map