Skip to content

Commit b67946a

Browse files
committed
Minor tweaks and add tests, bump to v2.0.1
1 parent 301d84a commit b67946a

File tree

15 files changed

+303
-33
lines changed

15 files changed

+303
-33
lines changed

.github/workflows/ci.yml

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ on:
33
pull_request:
44
push:
55
branches:
6-
- master
6+
- main
77

88
jobs:
99
build:
10-
name: Build and test
10+
name: Build
1111
runs-on: ${{ matrix.os }}
1212
strategy:
1313
fail-fast: false
@@ -22,10 +22,8 @@ jobs:
2222
- run: npm ci
2323
- run: npm run all
2424

25-
# merged into integration jobs below
26-
2725
test-cordova:
28-
name: Test (Cordova) on Ubuntu/macOS; iOS on macOS
26+
name: Test (Cordova)
2927
runs-on: ${{ matrix.os }}
3028
strategy:
3129
fail-fast: false
@@ -45,12 +43,21 @@ jobs:
4543
run: |
4644
cordova -v
4745
ionic info || true
46+
java -version
47+
javac -version
4848
- name: Build Android sample
4949
run: |
5050
ionic start testapp blank --cordova --type angular --no-link --no-git --no-interactive --confirm
5151
cd testapp
5252
ionic cordova platform add android@latest
5353
ionic cordova build android
54+
- name: Upload Android APK (Cordova)
55+
uses: actions/upload-artifact@v4
56+
with:
57+
name: android-apk-cordova-${{ matrix.os }}
58+
path: |
59+
testapp/platforms/android/app/build/outputs/apk/**/*.apk
60+
retention-days: 7
5461
- name: Build iOS sample (macOS only)
5562
if: ${{ matrix.os == 'macos-latest' }}
5663
run: |
@@ -59,8 +66,10 @@ jobs:
5966
ionic cordova build ios --no-interactive -- --buildFlag="-sdk iphonesimulator"
6067
6168
test-capacitor:
62-
name: Test (Capacitor) on Ubuntu/macOS; iOS on macOS
69+
name: Test (Capacitor)
6370
runs-on: ${{ matrix.os }}
71+
env:
72+
CAPACITOR_ANDROID_STUDIO_PATH: /usr/bin/false
6473
strategy:
6574
fail-fast: false
6675
matrix:
@@ -79,16 +88,34 @@ jobs:
7988
run: |
8089
cap --version
8190
ionic info || true
91+
java -version
92+
javac -version
8293
- name: Build Android sample
8394
run: |
8495
ionic start testapp blank --type angular --no-link --no-git --no-interactive --confirm
8596
cd testapp
8697
ionic cap add android
87-
ionic cap build android
98+
ionic cap build android --no-open
99+
- name: Inject Gradle Java 17 (non-Windows)
100+
if: runner.os != 'Windows'
101+
shell: bash
102+
run: bash scripts/inject-gradle-java17.sh testapp
103+
- name: Inject Gradle Java 17 (Windows)
104+
if: runner.os == 'Windows'
105+
shell: pwsh
106+
run: pwsh -File scripts/inject-gradle-java17.ps1 -AppPath testapp
107+
- name: Upload Android APK (Capacitor)
108+
uses: actions/upload-artifact@v4
109+
with:
110+
name: android-apk-${{ runner.os }}
111+
path: |
112+
testapp/android/app/build/outputs/apk/debug/*.apk
113+
testapp\android\app\build\outputs\apk\debug\*.apk
114+
retention-days: 7
88115
- name: Build iOS sample (macOS only)
89116
if: ${{ matrix.os == 'macos-latest' }}
90117
run: |
91118
cd testapp
92119
ionic cap add ios
93120
npx cap sync ios
94-
xcodebuild -workspace ios/App/App.xcworkspace -scheme App -sdk iphonesimulator -configuration Debug build
121+
xcodebuild -workspace ios/App/App.xcworkspace -scheme App -sdk iphonesimulator -configuration Debug build

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ Set up your GitHub Actions workflow with Capacitor or Cordova environment. Suppo
1010
# Capacitor (default)
1111
- name: Setup environment
1212
uses: coturiv/setup-ionic@v2
13-
with:
14-
legacy: false
15-
capacitor-version: latest
16-
java-version: 17
1713
1814
- name: Build Android
1915
run: |

__tests__/installer.test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import * as fs from 'fs'
2+
import * as path from 'path'
3+
import * as os from 'os'
4+
5+
jest.mock(
6+
'@actions/cache',
7+
() => ({
8+
restoreCache: jest.fn(async () => void 0),
9+
saveCache: jest.fn(async () => 0)
10+
}),
11+
{virtual: true}
12+
)
13+
14+
jest.mock(
15+
'@actions/core',
16+
() => ({
17+
addPath: jest.fn(() => void 0),
18+
exportVariable: jest.fn(() => void 0),
19+
info: jest.fn(() => void 0),
20+
setFailed: jest.fn(() => void 0)
21+
}),
22+
{virtual: true}
23+
)
24+
25+
jest.mock(
26+
'child_process',
27+
() => ({
28+
exec: (cmd: string, cb: (err: any, stdout: any, stderr: any) => void) => {
29+
if (cmd.includes('which javac')) cb(null, '/usr/bin/javac\n', '')
30+
else if (cmd.includes('brew --prefix')) cb(null, '/opt/homebrew\n', '')
31+
else if (cmd.includes('where.exe java'))
32+
cb(null, 'C:/Program Files/Eclipse Adoptium/jdk-17/bin/java.exe\n', '')
33+
else cb(null, '', '')
34+
}
35+
}),
36+
{virtual: true}
37+
)
38+
39+
jest.mock(
40+
'@actions/tool-cache',
41+
() => ({
42+
find: jest.fn(() => ''),
43+
cacheDir: jest.fn(async () => '/cached')
44+
}),
45+
{virtual: true}
46+
)
47+
48+
import {
49+
installJava,
50+
installNpmPkg,
51+
restoreCaches,
52+
saveCaches
53+
} from '../src/installer'
54+
55+
const core = require('@actions/core')
56+
const cache = require('@actions/cache')
57+
58+
describe('installer', () => {
59+
const originalPlatform = process.platform
60+
afterEach(() => {
61+
Object.defineProperty(process, 'platform', {value: originalPlatform})
62+
jest.clearAllMocks()
63+
})
64+
65+
test('installJava sets env on linux', async () => {
66+
Object.defineProperty(process, 'platform', {value: 'linux'})
67+
await installJava('17')
68+
expect(core.exportVariable).toHaveBeenCalledWith('JAVA_HOME', '/usr')
69+
expect(core.addPath).toHaveBeenCalledWith('/usr/bin')
70+
})
71+
72+
test('installNpmPkg caches bin path', async () => {
73+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'installer-test-'))
74+
await installNpmPkg('@capacitor/cli', 'latest')
75+
const call = (core.addPath as any).mock.calls.find((c: any[]) => {
76+
const arg = String(c[0])
77+
return arg.includes('node_modules') && arg.includes('.bin')
78+
})
79+
expect(call).toBeTruthy()
80+
fs.rmdirSync(tmp, {recursive: true})
81+
})
82+
83+
test('android sdk cache restore/save with env root', async () => {
84+
const sdkRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'android-sdk-'))
85+
fs.mkdirSync(path.join(sdkRoot, 'platforms'))
86+
fs.mkdirSync(path.join(sdkRoot, 'build-tools'))
87+
process.env.ANDROID_SDK_ROOT = sdkRoot
88+
await restoreCaches()
89+
await saveCaches()
90+
const restoreCall = (cache.restoreCache as any).mock.calls.find(
91+
(c: any[]) => Array.isArray(c[0]) && c[0][0] === sdkRoot
92+
)
93+
const saveCall = (cache.saveCache as any).mock.calls.find(
94+
(c: any[]) => Array.isArray(c[0]) && c[0][0] === sdkRoot
95+
)
96+
expect(restoreCall).toBeTruthy()
97+
expect(saveCall).toBeTruthy()
98+
fs.rmdirSync(sdkRoot, {recursive: true})
99+
delete process.env.ANDROID_SDK_ROOT
100+
})
101+
})

__tests__/main.test.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
1-
import * as process from 'process'
2-
import * as cp from 'child_process'
3-
import * as path from 'path'
1+
jest.mock('../src/installer', () => ({
2+
installCordova: jest.fn(async () => void 0),
3+
installIonic: jest.fn(async () => void 0),
4+
installCapacitor: jest.fn(async () => void 0),
5+
installJava: jest.fn(async () => void 0),
6+
installPods: jest.fn(async () => void 0),
7+
logInstalledInfo: jest.fn(async () => void 0),
8+
restoreCaches: jest.fn(async () => void 0),
9+
saveCaches: jest.fn(async () => void 0)
10+
}))
411

5-
// shows how the runner will run a javascript action with env / stdout protocol
6-
test('test runs', () => {
7-
// const ip = path.join(__dirname, '..', 'lib', 'main.js');
8-
// const options: cp.ExecSyncOptions = {
9-
// env: process.env
10-
// };
11-
// console.log(cp.execSync(`node ${ip}`, options).toString());
12+
jest.mock('@actions/core', () => ({
13+
getInput: jest.fn((name: string) => {
14+
if (name === 'legacy') return 'false'
15+
if (name === 'capacitor-version') return 'latest'
16+
if (name === 'ionic-version') return 'latest'
17+
if (name === 'install-java') return 'true'
18+
if (name === 'java-version') return '17'
19+
if (name === 'install-pods') return 'false'
20+
return ''
21+
}),
22+
info: jest.fn(() => void 0),
23+
setFailed: jest.fn(() => void 0)
24+
}))
25+
26+
test('runs main and calls installers', async () => {
27+
await import('../src/main')
28+
await new Promise(res => setTimeout(res, 0))
29+
const inst = await import('../src/installer')
30+
expect(inst.restoreCaches as any).toHaveBeenCalled()
31+
expect(inst.installCapacitor as any).toHaveBeenCalledWith('latest')
32+
expect(inst.installIonic as any).toHaveBeenCalledWith('latest')
33+
expect(inst.installJava as any).toHaveBeenCalledWith('17')
34+
expect(inst.installPods as any).not.toHaveBeenCalled()
35+
expect(inst.saveCaches as any).toHaveBeenCalled()
1236
})

__tests__/post.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
jest.mock('../src/installer', () => ({
2+
saveCaches: jest.fn(async () => void 0)
3+
}))
4+
5+
jest.mock('@actions/core', () => ({
6+
info: jest.fn(() => void 0)
7+
}))
8+
9+
test('runs post and calls saveCaches', async () => {
10+
await import('../src/post')
11+
await new Promise(res => setTimeout(res, 0))
12+
const inst = await import('../src/installer')
13+
expect(inst.saveCaches as any).toHaveBeenCalled()
14+
})
15+
16+
test('logs error when saveCaches throws', async () => {
17+
jest.resetModules()
18+
const inst = await import('../src/installer')
19+
;(inst.saveCaches as any).mockImplementationOnce(async () => {
20+
throw new Error('boom')
21+
})
22+
const core = await import('@actions/core')
23+
await import('../src/post')
24+
await new Promise(res => setTimeout(res, 0))
25+
expect(core.info as any).toHaveBeenCalledWith('boom')
26+
})

action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ inputs:
1717
description: Use Cordova (true) or Capacitor (false)
1818
default: 'false'
1919
java-version:
20-
description: JDK major version to install on Linux/macOS (e.g. 17, 21)
20+
description: JDK major version to install (e.g. 17, 21)
2121
default: '17'
2222
install-java:
2323
description: Whether to install Java
@@ -29,3 +29,4 @@ inputs:
2929
runs:
3030
using: 'node20'
3131
main: 'dist/index.js'
32+
post: 'dist/post.js'

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/post.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "setup-ionic",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"private": true,
55
"description": "Set up iOS and Android environment for Capacitor or Cordova projects.",
66
"main": "dist/index.js",
@@ -9,9 +9,12 @@
99
"format": "prettier --write **/*.ts",
1010
"format-check": "prettier --check **/*.ts",
1111
"lint": "eslint src/**/*.ts",
12-
"pack": "ncc build src/main.ts -m -q",
12+
"pack": "bash scripts/pack.sh",
1313
"test": "jest",
14-
"all": "npm run build && npm run format && npm run lint && npm run pack && npm test"
14+
"test:src": "jest --coverage --collectCoverageFrom=\"src/**/*.ts\"",
15+
"test:watch": "jest --watchAll",
16+
"test:ci": "jest --runInBand",
17+
"all": "npm run build && npm run format && npm run lint && npm run pack && npm run test:ci"
1518
},
1619
"repository": {
1720
"type": "git",

scripts/inject-gradle-java17.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
param(
2+
[string]$AppPath = "testapp"
3+
)
4+
5+
$gradleFile = Join-Path $AppPath "android\build.gradle"
6+
if (-not (Test-Path $gradleFile)) {
7+
Write-Error "Gradle file not found: $gradleFile"
8+
exit 1
9+
}
10+
11+
$snippet = @"
12+
subprojects { subproject ->
13+
afterEvaluate {
14+
if (subproject.plugins.hasPlugin('com.android.library') || subproject.plugins.hasPlugin('com.android.application')) {
15+
subproject.android.compileOptions {
16+
sourceCompatibility JavaVersion.VERSION_17
17+
targetCompatibility JavaVersion.VERSION_17
18+
}
19+
}
20+
}
21+
}
22+
"@
23+
24+
Add-Content -Path $gradleFile -Value $snippet
25+
26+
Push-Location (Join-Path $AppPath "android")
27+
./gradlew.bat assembleDebug
28+
Pop-Location

0 commit comments

Comments
 (0)