1import * as WarningAggregator from '../../utils/warnings';
2import {
3  getVersionCode,
4  getVersionName,
5  setMinBuildScriptExtVersion,
6  setVersionCode,
7  setVersionName,
8} from '../Version';
9
10// TODO: use fixtures for manifest/build.gradle instead of inline strings
11
12const EXAMPLE_BUILD_GRADLE = `
13android {
14    compileSdkVersion rootProject.ext.compileSdkVersion
15    buildToolsVersion rootProject.ext.buildToolsVersion
16
17    defaultConfig {
18        applicationId "com.helloworld"
19        minSdkVersion rootProject.ext.minSdkVersion
20        targetSdkVersion rootProject.ext.targetSdkVersion
21        versionCode 1
22        versionName "1.2.3"
23    }
24}
25`;
26
27const EXAMPLE_BUILD_GRADLE_2 = `
28android {
29    compileSdkVersion rootProject.ext.compileSdkVersion
30    buildToolsVersion rootProject.ext.buildToolsVersion
31
32    defaultConfig {
33        applicationId "com.helloworld"
34        minSdkVersion rootProject.ext.minSdkVersion
35        targetSdkVersion rootProject.ext.targetSdkVersion
36        versionCode 4
37        versionName "2.0"
38    }
39}
40`;
41
42const EXAMPLE_PROJECT_BUILD_GRADLE = `
43buildscript {
44    ext {
45        buildToolsVersion = "29.0.3"
46        minSdkVersion = 21
47        compileSdkVersion = 30
48        targetSdkVersion = 30
49        oddFormat = 30.2
50    }
51    repositories {
52    }
53    dependencies {
54    }
55}
56
57allprojects {
58    repositories {
59        mavenLocal()
60        maven {
61            // Android JSC is installed from npm
62            url(new File(["node", "--print", "require.resolve('jsc-android/package.json')"].execute(null, rootDir).text.trim(), "../dist"))
63        }
64    }
65}
66`;
67
68describe('versionName', () => {
69  it(`returns null if no version is provided`, () => {
70    expect(getVersionName({})).toBe(null);
71  });
72
73  it(`returns the version name if provided`, () => {
74    expect(getVersionName({ version: '1.2.3' })).toBe('1.2.3');
75  });
76
77  it(`sets the version name in build.gradle if version name is given`, () => {
78    expect(setVersionName({ version: '1.2.3' }, EXAMPLE_BUILD_GRADLE)).toMatch(
79      'versionName "1.2.3"'
80    );
81  });
82
83  it(`replaces provided version name in build.gradle if version name is not the default`, () => {
84    expect(setVersionName({ version: '1.2.3' }, EXAMPLE_BUILD_GRADLE_2)).toMatch(
85      'versionName "1.2.3"'
86    );
87  });
88});
89
90describe('versionCode', () => {
91  it(`returns 1 if no version code is provided`, () => {
92    expect(getVersionCode({})).toBe(1);
93  });
94
95  it(`returns the version code if provided`, () => {
96    expect(getVersionCode({ android: { versionCode: 5 } })).toBe(5);
97  });
98
99  it(`sets the version code in build.gradle if version code is given`, () => {
100    expect(setVersionCode({ android: { versionCode: 5 } }, EXAMPLE_BUILD_GRADLE)).toMatch(
101      'versionCode 5'
102    );
103  });
104
105  it(`replaces provided version code in build.gradle if version code is given`, () => {
106    expect(setVersionCode({ android: { versionCode: 5 } }, EXAMPLE_BUILD_GRADLE_2)).toMatch(
107      'versionCode 5'
108    );
109  });
110});
111
112describe(setMinBuildScriptExtVersion, () => {
113  beforeEach(() => {
114    // @ts-ignore: jest
115    // eslint-disable-next-line import/namespace
116    WarningAggregator.addWarningAndroid = jest.fn();
117  });
118
119  it(`sets the minSdkVersion in build.gradle if minSdkVersion is given`, () => {
120    expect(
121      setMinBuildScriptExtVersion(EXAMPLE_PROJECT_BUILD_GRADLE, {
122        name: 'minSdkVersion',
123        minVersion: 22,
124      })
125    ).toMatch(/minSdkVersion = 22/);
126    expect(WarningAggregator.addWarningAndroid).not.toHaveBeenCalled();
127  });
128
129  it(`sets the oddFormat in build.gradle if oddFormat is given`, () => {
130    expect(
131      setMinBuildScriptExtVersion(EXAMPLE_PROJECT_BUILD_GRADLE, {
132        name: 'oddFormat',
133        minVersion: 30.3,
134      })
135    ).toMatch(/oddFormat = 30\.3/);
136    expect(WarningAggregator.addWarningAndroid).not.toHaveBeenCalled();
137  });
138  it(`does not change the compileSdkVersion in build.gradle if compileSdkVersion is lower than the existing value`, () => {
139    expect(
140      setMinBuildScriptExtVersion(EXAMPLE_PROJECT_BUILD_GRADLE, {
141        name: 'compileSdkVersion',
142        minVersion: 12,
143      })
144    ).toBe(EXAMPLE_PROJECT_BUILD_GRADLE);
145    expect(WarningAggregator.addWarningAndroid).not.toHaveBeenCalled();
146  });
147  it(`warns when it cannot find the requested value`, () => {
148    expect(
149      setMinBuildScriptExtVersion(EXAMPLE_PROJECT_BUILD_GRADLE, {
150        name: 'foobar',
151        minVersion: 12,
152      })
153    ).toBe(EXAMPLE_PROJECT_BUILD_GRADLE);
154    expect(WarningAggregator.addWarningAndroid).toBeCalledWith(
155      'withBuildScriptExtVersion',
156      'Cannot set minimum buildscript.ext.foobar version because the property "foobar" cannot be found or does not have a numeric value.'
157    );
158  });
159  it(`does warns when targeting a property with a string value`, () => {
160    expect(
161      setMinBuildScriptExtVersion(EXAMPLE_PROJECT_BUILD_GRADLE, {
162        name: 'buildToolsVersion',
163        minVersion: 12,
164      })
165    ).toBe(EXAMPLE_PROJECT_BUILD_GRADLE);
166    expect(WarningAggregator.addWarningAndroid).toBeCalledWith(
167      'withBuildScriptExtVersion',
168      'Cannot set minimum buildscript.ext.buildToolsVersion version because the property "buildToolsVersion" cannot be found or does not have a numeric value.'
169    );
170  });
171});
172