-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
validate icon PNGs before running build
- Loading branch information
Showing
15 changed files
with
768 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
* text eol=lf | ||
*.png binary | ||
*.jpg binary | ||
|
||
**/yarn.lock linguist-generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { Platform, Workflow } from '@expo/eas-build-job'; | ||
import { ExitError } from '@oclif/core/lib/errors'; | ||
import path from 'path'; | ||
import { instance, mock, when } from 'ts-mockito'; | ||
|
||
import { CommonContext } from '../context'; | ||
import { validateIconForManagedProjectAsync } from '../validate'; | ||
|
||
const fixturesPath = path.join(__dirname, 'fixtures'); | ||
|
||
describe(validateIconForManagedProjectAsync, () => { | ||
it('does not validate the icons for generic projects', async () => { | ||
const ctxMock = mock<CommonContext<Platform.ANDROID>>(); | ||
when(ctxMock.workflow).thenReturn(Workflow.GENERIC); | ||
const ctx = instance(ctxMock); | ||
await expect(validateIconForManagedProjectAsync(ctx)).resolves.not.toThrow(); | ||
}); | ||
|
||
describe(Platform.ANDROID, () => { | ||
it('exits if foregroundImage is not a file with .png extension', async () => { | ||
const ctxMock = mock<CommonContext<Platform.ANDROID>>(); | ||
when(ctxMock.workflow).thenReturn(Workflow.MANAGED); | ||
when(ctxMock.platform).thenReturn(Platform.ANDROID); | ||
when(ctxMock.exp).thenReturn({ | ||
name: 'blah', | ||
slug: 'blah', | ||
android: { adaptiveIcon: { foregroundImage: 'assets/icon.jpg' } }, | ||
}); | ||
const ctx = instance(ctxMock); | ||
|
||
await expect(validateIconForManagedProjectAsync(ctx)).rejects.toThrow(ExitError); | ||
}); | ||
|
||
it('exits if foregroundImage is not a png file', async () => { | ||
const ctxMock = mock<CommonContext<Platform.ANDROID>>(); | ||
when(ctxMock.workflow).thenReturn(Workflow.MANAGED); | ||
when(ctxMock.platform).thenReturn(Platform.ANDROID); | ||
when(ctxMock.exp).thenReturn({ | ||
name: 'blah', | ||
slug: 'blah', | ||
android: { adaptiveIcon: { foregroundImage: path.join(fixturesPath, 'icon-jpg.png') } }, | ||
}); | ||
const ctx = instance(ctxMock); | ||
|
||
await expect(validateIconForManagedProjectAsync(ctx)).rejects.toThrow(ExitError); | ||
}); | ||
|
||
it('does not throw if foregroundImage is a png file', async () => { | ||
const ctxMock = mock<CommonContext<Platform.ANDROID>>(); | ||
when(ctxMock.workflow).thenReturn(Workflow.MANAGED); | ||
when(ctxMock.platform).thenReturn(Platform.ANDROID); | ||
when(ctxMock.exp).thenReturn({ | ||
name: 'blah', | ||
slug: 'blah', | ||
android: { adaptiveIcon: { foregroundImage: path.join(fixturesPath, 'icon-alpha.png') } }, | ||
}); | ||
const ctx = instance(ctxMock); | ||
|
||
await expect(validateIconForManagedProjectAsync(ctx)).resolves.not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe(Platform.IOS, () => { | ||
it('exits if foregroundImage is not a file with .png extension', async () => { | ||
const ctxMock = mock<CommonContext<Platform.IOS>>(); | ||
when(ctxMock.workflow).thenReturn(Workflow.MANAGED); | ||
when(ctxMock.platform).thenReturn(Platform.IOS); | ||
when(ctxMock.exp).thenReturn({ | ||
name: 'blah', | ||
slug: 'blah', | ||
ios: { icon: path.join(fixturesPath, 'assets/icon.jpg') }, | ||
}); | ||
const ctx = instance(ctxMock); | ||
|
||
await expect(validateIconForManagedProjectAsync(ctx)).rejects.toThrow(ExitError); | ||
}); | ||
|
||
it('exits if icon is not a png file', async () => { | ||
const ctxMock = mock<CommonContext<Platform.IOS>>(); | ||
when(ctxMock.workflow).thenReturn(Workflow.MANAGED); | ||
when(ctxMock.platform).thenReturn(Platform.IOS); | ||
when(ctxMock.exp).thenReturn({ | ||
name: 'blah', | ||
slug: 'blah', | ||
ios: { icon: path.join(fixturesPath, 'icon-jpg.png') }, | ||
}); | ||
const ctx = instance(ctxMock); | ||
|
||
await expect(validateIconForManagedProjectAsync(ctx)).rejects.toThrow(ExitError); | ||
}); | ||
|
||
it('exits if icon has alpha channel (transparency)', async () => { | ||
const ctxMock = mock<CommonContext<Platform.IOS>>(); | ||
when(ctxMock.workflow).thenReturn(Workflow.MANAGED); | ||
when(ctxMock.platform).thenReturn(Platform.IOS); | ||
when(ctxMock.exp).thenReturn({ | ||
name: 'blah', | ||
slug: 'blah', | ||
ios: { icon: path.join(fixturesPath, 'icon-alpha.png') }, | ||
}); | ||
const ctx = instance(ctxMock); | ||
|
||
await expect(validateIconForManagedProjectAsync(ctx)).rejects.toThrow(ExitError); | ||
}); | ||
|
||
it('does not throw if icon is a png file and does not have alpha channel', async () => { | ||
const ctxMock = mock<CommonContext<Platform.IOS>>(); | ||
when(ctxMock.workflow).thenReturn(Workflow.MANAGED); | ||
when(ctxMock.platform).thenReturn(Platform.IOS); | ||
when(ctxMock.exp).thenReturn({ | ||
name: 'blah', | ||
slug: 'blah', | ||
ios: { icon: path.join(fixturesPath, 'icon-no-alpha.png') }, | ||
}); | ||
const ctx = instance(ctxMock); | ||
|
||
await expect(validateIconForManagedProjectAsync(ctx)).resolves.not.toThrow(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import express from 'express'; | ||
import http from 'http'; | ||
import path from 'path'; | ||
|
||
import { ImageNonPngError, ImageTransparencyError, ensurePNGIsNotTransparentAsync } from '../image'; | ||
|
||
const TEST_SERVER_PORT = 2137; | ||
|
||
const fixturesPath = path.join(__dirname, 'fixtures'); | ||
const transparentPngPath = path.join(fixturesPath, 'icon-alpha.png'); | ||
const nonTransparentPngPath = path.join(fixturesPath, 'icon-no-alpha.png'); | ||
const jpgPath = path.join(fixturesPath, 'icon.jpg'); | ||
const nonTransparentPngURL = `http://127.0.0.1:${TEST_SERVER_PORT}/icon-alpha.png`; | ||
|
||
describe(ensurePNGIsNotTransparentAsync, () => { | ||
describe('local paths', () => { | ||
it('throws an error for non-PNG files', async () => { | ||
await expect(ensurePNGIsNotTransparentAsync(jpgPath)).rejects.toThrowError(ImageNonPngError); | ||
}); | ||
|
||
it('throws an error for transparent PNGs', async () => { | ||
await expect(ensurePNGIsNotTransparentAsync(transparentPngPath)).rejects.toThrowError( | ||
ImageTransparencyError | ||
); | ||
}); | ||
|
||
it('does not throw for non transparent PNGs', async () => { | ||
await expect(ensurePNGIsNotTransparentAsync(nonTransparentPngPath)).resolves.not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('remote URLs', () => { | ||
let server: http.Server; | ||
|
||
beforeAll(async () => { | ||
await new Promise<void>(res => { | ||
const app = express(); | ||
app.use(express.static(fixturesPath)); | ||
server = app.listen(TEST_SERVER_PORT, () => { | ||
res(); | ||
}); | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await new Promise<void>(res => { | ||
server.close(() => { | ||
res(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('works with remote URLs', async () => { | ||
await expect(ensurePNGIsNotTransparentAsync(nonTransparentPngURL)).rejects.toThrowError( | ||
ImageTransparencyError | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.