Skip to content

Commit 4fb4913

Browse files
committed
test: add validation for CapacitorUpdaterPlugin in iOS projects
1 parent 9790b02 commit 4fb4913

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

src/utils.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,15 @@ function hasUpdaterInText(content: string | undefined): boolean {
21272127
return /@capgo\/capacitor-updater|CapgoCapacitorUpdater|CapacitorUpdaterPlugin/.test(content)
21282128
}
21292129

2130+
function hasUpdaterInCapacitorConfigJson(filePath: string): boolean {
2131+
const config = readJsonFileSafely(filePath)
2132+
if (!config)
2133+
return false
2134+
2135+
const packageClassList = config.packageClassList
2136+
return Array.isArray(packageClassList) && packageClassList.includes('CapacitorUpdaterPlugin')
2137+
}
2138+
21302139
function resolvePackageJsonLocation(rootDir: string, packageJsonPath?: string): string {
21312140
if (!packageJsonPath)
21322141
return join(rootDir, PACKNAME)
@@ -2140,7 +2149,8 @@ function resolvePackageJsonLocation(rootDir: string, packageJsonPath?: string):
21402149
* (no dependency declaration, installed package, or native references). `shouldCheck` is `true`
21412150
* as soon as any signal indicates updater should be wired, then both dependency definitions
21422151
* (`Podfile` or SPM `Package.swift`) and generated native outputs (`Podfile.lock`,
2143-
* `Package.resolved`, or `capacitor.plugins.json`) must include updater markers for `valid` to be `true`.
2152+
* `capacitor.plugins.json`, or `ios/App/App/capacitor.config.json`) must include
2153+
* updater markers for `valid` to be `true`.
21442154
*/
21452155
export function validateIosUpdaterSync(
21462156
rootDir: string = cwd(),
@@ -2176,14 +2186,13 @@ export function validateIosUpdaterSync(
21762186
const hasDependencyEntry = hasUpdaterInText(podfileContent) || hasUpdaterInText(spmPackageContent)
21772187

21782188
const podfileLockPath = join(iosAppPath, 'Podfile.lock')
2179-
const packageResolvedPath = join(iosAppPath, 'App.xcworkspace', 'xcshareddata', 'swiftpm', 'Package.resolved')
21802189
const capacitorPluginsPath = join(iosAppPath, 'App', 'capacitor.plugins.json')
2190+
const capacitorConfigJsonPath = join(iosAppPath, 'App', 'capacitor.config.json')
21812191
const podfileLockContent = existsSync(podfileLockPath) ? readFileSync(podfileLockPath, 'utf-8') : undefined
2182-
const packageResolvedContent = existsSync(packageResolvedPath) ? readFileSync(packageResolvedPath, 'utf-8') : undefined
21832192
const capacitorPluginsContent = existsSync(capacitorPluginsPath) ? readFileSync(capacitorPluginsPath, 'utf-8') : undefined
21842193
const hasNativeProjectEntry = hasUpdaterInText(podfileLockContent)
2185-
|| hasUpdaterInText(packageResolvedContent)
21862194
|| hasUpdaterInText(capacitorPluginsContent)
2195+
|| hasUpdaterInCapacitorConfigJson(capacitorConfigJsonPath)
21872196

21882197
const shouldCheck = updaterDeclaredInPackageJson
21892198
|| updaterPresentInNodeModules
@@ -2203,7 +2212,7 @@ export function validateIosUpdaterSync(
22032212
details.push(`Missing @capgo/capacitor-updater in iOS dependency files (${podfilePath} or ${spmPackagePath})`)
22042213
}
22052214
if (!hasNativeProjectEntry) {
2206-
details.push(`Missing @capgo/capacitor-updater in iOS native project outputs (${podfileLockPath}, ${packageResolvedPath}, or ${capacitorPluginsPath})`)
2215+
details.push(`Missing @capgo/capacitor-updater in iOS native project outputs (${podfileLockPath}, ${capacitorPluginsPath}, or ${capacitorConfigJsonPath})`)
22072216
}
22082217

22092218
return {

test/test-ios-updater-sync-validation.mjs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,69 @@ await test('valid iOS project returns shouldCheck=true and valid=true', () => {
6060
}
6161
})
6262

63+
await test('spm iOS project with packageClassList returns shouldCheck=true and valid=true', () => {
64+
const root = makeProjectDir()
65+
try {
66+
writeFile(join(root, 'package.json'), JSON.stringify({
67+
dependencies: {
68+
'@capgo/capacitor-updater': '^7.0.0',
69+
},
70+
}))
71+
72+
const iosRoot = join(root, 'ios', 'App')
73+
writeFile(
74+
join(iosRoot, 'CapApp-SPM', 'Package.swift'),
75+
`.package(name: "CapgoCapacitorUpdater", path: "../../../node_modules/@capgo/capacitor-updater"),\n`,
76+
)
77+
writeFile(
78+
join(iosRoot, 'App', 'capacitor.config.json'),
79+
JSON.stringify({
80+
packageClassList: ['CapacitorUpdaterPlugin'],
81+
}),
82+
)
83+
84+
const result = validateIosUpdaterSync(root)
85+
assert(result.shouldCheck === true, 'Expected shouldCheck=true')
86+
assert(result.valid === true, 'Expected valid=true')
87+
assert(result.details.length === 0, 'Expected no details')
88+
}
89+
finally {
90+
rmSync(root, { recursive: true, force: true })
91+
}
92+
})
93+
94+
await test('spm iOS project ignores CapacitorUpdaterPlugin outside packageClassList', () => {
95+
const root = makeProjectDir()
96+
try {
97+
writeFile(join(root, 'package.json'), JSON.stringify({
98+
dependencies: {
99+
'@capgo/capacitor-updater': '^7.0.0',
100+
},
101+
}))
102+
103+
const iosRoot = join(root, 'ios', 'App')
104+
writeFile(
105+
join(iosRoot, 'CapApp-SPM', 'Package.swift'),
106+
`.package(name: "CapgoCapacitorUpdater", path: "../../../node_modules/@capgo/capacitor-updater"),\n`,
107+
)
108+
writeFile(
109+
join(iosRoot, 'App', 'capacitor.config.json'),
110+
JSON.stringify({
111+
packageClassList: ['OtherPlugin'],
112+
note: 'CapacitorUpdaterPlugin',
113+
}),
114+
)
115+
116+
const result = validateIosUpdaterSync(root)
117+
assert(result.shouldCheck === true, 'Expected shouldCheck=true')
118+
assert(result.valid === false, 'Expected valid=false')
119+
assert(result.details.some(detail => detail.includes('native project outputs')), 'Expected native-outputs detail')
120+
}
121+
finally {
122+
rmSync(root, { recursive: true, force: true })
123+
}
124+
})
125+
63126
await test('missing dependency file entries fails validation', () => {
64127
const root = makeProjectDir()
65128
try {

0 commit comments

Comments
 (0)