|
| 1 | +import { withAppBuildGradle, withProjectBuildGradle } from '@expo/config-plugins'; |
| 2 | + |
| 3 | +import { warnOnce } from '../../plugin/src/utils'; |
| 4 | +import type { SentryAndroidGradlePluginOptions } from '../../plugin/src/withSentryAndroidGradlePlugin'; |
| 5 | +import { withSentryAndroidGradlePlugin } from '../../plugin/src/withSentryAndroidGradlePlugin'; |
| 6 | + |
| 7 | +jest.mock('@expo/config-plugins', () => ({ |
| 8 | + withProjectBuildGradle: jest.fn(), |
| 9 | + withAppBuildGradle: jest.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock('../../plugin/src/utils', () => ({ |
| 13 | + warnOnce: jest.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +const mockedBuildGradle = ` |
| 17 | +buildscript { |
| 18 | + dependencies { |
| 19 | + classpath('otherDependency') |
| 20 | + } |
| 21 | +} |
| 22 | +`; |
| 23 | + |
| 24 | +const mockedAppBuildGradle = ` |
| 25 | +apply plugin: "somePlugin" |
| 26 | +react { |
| 27 | +} |
| 28 | +android { |
| 29 | +} |
| 30 | +dependencies { |
| 31 | +} |
| 32 | +`; |
| 33 | + |
| 34 | +describe('withSentryAndroidGradlePlugin', () => { |
| 35 | + const mockConfig = { |
| 36 | + name: 'test-app', |
| 37 | + slug: 'test-app', |
| 38 | + modResults: { contents: '' }, |
| 39 | + }; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + jest.clearAllMocks(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('adds the Sentry plugin to build.gradle when enableAndroidGradlePlugin is enabled', () => { |
| 46 | + const version = '4.14.1'; |
| 47 | + const options: SentryAndroidGradlePluginOptions = { enableAndroidGradlePlugin: true }; |
| 48 | + |
| 49 | + (withProjectBuildGradle as jest.Mock).mockImplementation((config, callback) => { |
| 50 | + const projectBuildGradle = { |
| 51 | + modResults: { language: 'groovy', contents: mockedBuildGradle }, |
| 52 | + }; |
| 53 | + const modified = callback(projectBuildGradle); |
| 54 | + return modified; |
| 55 | + }); |
| 56 | + |
| 57 | + withSentryAndroidGradlePlugin(mockConfig, options); |
| 58 | + |
| 59 | + expect(withProjectBuildGradle).toHaveBeenCalled(); |
| 60 | + expect(withProjectBuildGradle).toHaveBeenCalledWith(expect.any(Object), expect.any(Function)); |
| 61 | + |
| 62 | + const calledCallback = (withProjectBuildGradle as jest.Mock).mock.calls[0][1]; |
| 63 | + const modifiedGradle = calledCallback({ |
| 64 | + modResults: { language: 'groovy', contents: mockedBuildGradle }, |
| 65 | + }); |
| 66 | + |
| 67 | + expect(modifiedGradle.modResults.contents).toContain( |
| 68 | + `classpath("io.sentry:sentry-android-gradle-plugin:${version}")`, |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it('warnOnce if the Sentry plugin is already included in build.gradle', () => { |
| 73 | + const version = '4.14.1'; |
| 74 | + const includedBuildGradle = `dependencies { classpath("io.sentry:sentry-android-gradle-plugin:${version}")}`; |
| 75 | + const options: SentryAndroidGradlePluginOptions = { enableAndroidGradlePlugin: true }; |
| 76 | + |
| 77 | + (withProjectBuildGradle as jest.Mock).mockImplementation((config, callback) => { |
| 78 | + callback({ modResults: { language: 'groovy', contents: includedBuildGradle } }); |
| 79 | + }); |
| 80 | + |
| 81 | + withSentryAndroidGradlePlugin(mockConfig, options); |
| 82 | + |
| 83 | + expect(warnOnce).toHaveBeenCalledWith( |
| 84 | + 'sentry-android-gradle-plugin dependency in already in android/build.gradle.', |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it('warnOnce if failed to modify build.gradle', () => { |
| 89 | + const invalidBuildGradle = `android {}`; |
| 90 | + const options: SentryAndroidGradlePluginOptions = { enableAndroidGradlePlugin: true }; |
| 91 | + |
| 92 | + (withProjectBuildGradle as jest.Mock).mockImplementation((config, callback) => { |
| 93 | + callback({ modResults: { language: 'groovy', contents: invalidBuildGradle } }); |
| 94 | + }); |
| 95 | + |
| 96 | + withSentryAndroidGradlePlugin(mockConfig, options); |
| 97 | + |
| 98 | + expect(warnOnce).toHaveBeenCalledWith( |
| 99 | + 'Failed to inject the dependency. Could not find `dependencies` in build.gradle.', |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it('adds the Sentry plugin configuration to app/build.gradle', () => { |
| 104 | + const options: SentryAndroidGradlePluginOptions = { |
| 105 | + autoUploadProguardMapping: true, |
| 106 | + includeProguardMapping: true, |
| 107 | + dexguardEnabled: false, |
| 108 | + uploadNativeSymbols: true, |
| 109 | + autoUploadNativeSymbols: true, |
| 110 | + includeNativeSources: false, |
| 111 | + includeSourceContext: true, |
| 112 | + }; |
| 113 | + (withProjectBuildGradle as jest.Mock).mockImplementation((config, callback) => { |
| 114 | + const projectBuildGradle = { |
| 115 | + modResults: { language: 'groovy', contents: mockedBuildGradle }, |
| 116 | + }; |
| 117 | + const modified = callback(projectBuildGradle); |
| 118 | + return modified; |
| 119 | + }); |
| 120 | + (withAppBuildGradle as jest.Mock).mockImplementation((config, callback) => { |
| 121 | + const appBuildGradle = { |
| 122 | + modResults: { language: 'groovy', contents: mockedAppBuildGradle }, |
| 123 | + }; |
| 124 | + const modified = callback(appBuildGradle); |
| 125 | + return modified; |
| 126 | + }); |
| 127 | + |
| 128 | + withSentryAndroidGradlePlugin(mockConfig, options); |
| 129 | + |
| 130 | + expect(withAppBuildGradle).toHaveBeenCalled(); |
| 131 | + expect(withAppBuildGradle).toHaveBeenCalledWith(expect.any(Object), expect.any(Function)); |
| 132 | + |
| 133 | + const calledCallback = (withAppBuildGradle as jest.Mock).mock.calls[0][1]; |
| 134 | + const modifiedGradle = calledCallback({ |
| 135 | + modResults: { language: 'groovy', contents: mockedAppBuildGradle }, |
| 136 | + }); |
| 137 | + |
| 138 | + expect(modifiedGradle.modResults.contents).toContain('apply plugin: "io.sentry.android.gradle"'); |
| 139 | + expect(modifiedGradle.modResults.contents).toContain(` |
| 140 | + sentry { |
| 141 | + autoUploadProguardMapping = true |
| 142 | + includeProguardMapping = true |
| 143 | + dexguardEnabled = false |
| 144 | + uploadNativeSymbols = true |
| 145 | + autoUploadNativeSymbols = true |
| 146 | + includeNativeSources = false |
| 147 | + includeSourceContext = true |
| 148 | + tracingInstrumentation { |
| 149 | + enabled = false |
| 150 | + } |
| 151 | + autoInstallation { |
| 152 | + enabled = false |
| 153 | + } |
| 154 | + }`); |
| 155 | + }); |
| 156 | + |
| 157 | + it('warnOnce if modResults is missing in build.gradle', () => { |
| 158 | + (withProjectBuildGradle as jest.Mock).mockImplementation((config, callback) => { |
| 159 | + callback({}); |
| 160 | + }); |
| 161 | + |
| 162 | + withSentryAndroidGradlePlugin(mockConfig, {}); |
| 163 | + |
| 164 | + expect(warnOnce).toHaveBeenCalledWith('android/build.gradle content is missing or undefined.'); |
| 165 | + |
| 166 | + expect(withProjectBuildGradle).toHaveBeenCalled(); |
| 167 | + }); |
| 168 | + |
| 169 | + it('warnOnce if android/build.gradle is not Groovy', () => { |
| 170 | + (withProjectBuildGradle as jest.Mock).mockImplementation((config, callback) => { |
| 171 | + callback({ modResults: { language: 'kotlin', contents: mockedAppBuildGradle } }); |
| 172 | + }); |
| 173 | + |
| 174 | + withSentryAndroidGradlePlugin(mockConfig, {}); |
| 175 | + |
| 176 | + expect(warnOnce).toHaveBeenCalledWith( |
| 177 | + 'Cannot configure Sentry in android/build.gradle because it is not in Groovy.', |
| 178 | + ); |
| 179 | + |
| 180 | + expect(withProjectBuildGradle).toHaveBeenCalled(); |
| 181 | + }); |
| 182 | + |
| 183 | + it('warnOnce if app/build.gradle is not Groovy', () => { |
| 184 | + (withAppBuildGradle as jest.Mock).mockImplementation((config, callback) => { |
| 185 | + callback({ modResults: { language: 'kotlin', contents: mockedAppBuildGradle } }); |
| 186 | + }); |
| 187 | + |
| 188 | + withSentryAndroidGradlePlugin(mockConfig, {}); |
| 189 | + |
| 190 | + expect(warnOnce).toHaveBeenCalledWith( |
| 191 | + 'Cannot configure Sentry in android/app/build.gradle because it is not in Groovy.', |
| 192 | + ); |
| 193 | + |
| 194 | + expect(withAppBuildGradle).toHaveBeenCalled(); |
| 195 | + }); |
| 196 | +}); |
0 commit comments