Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions packages/worker/src/__unit__/build.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { BuildContext, Builders } from '@expo/build-tools';
import { Platform } from '@expo/eas-build-job';

import { build, warnOnUnknownEnvironment } from '../build';
import { Analytics } from '../external/analytics';

jest.mock('@expo/build-tools', () => {
const actual = jest.requireActual('@expo/build-tools');
return {
...actual,
Builders: {
androidBuilder: jest.fn(async () => ({})),
iosBuilder: jest.fn(async () => ({})),
},
};
});
jest.mock('../displayRuntimeInfo', () => ({ displayWorkerRuntimeInfo: jest.fn() }));
jest.mock('../workingdir', () => ({ cleanUpWorkingdir: jest.fn() }));
jest.mock('../external/analytics', () => ({
...jest.requireActual('../external/analytics'),
logProjectDependenciesAsync: jest.fn(),
}));

function createEnvironmentWarningCtx(environment?: string) {
return {
metadata: environment === undefined ? undefined : { environment },
markBuildPhaseHasWarnings: jest.fn(),
logger: { warn: jest.fn() },
};
}

describe(warnOnUnknownEnvironment.name, () => {
it('warns and marks the phase for an unknown environment', () => {
const ctx = createEnvironmentWarningCtx('staging');
warnOnUnknownEnvironment(ctx);
expect(ctx.markBuildPhaseHasWarnings).toHaveBeenCalled();
expect(ctx.logger.warn).toHaveBeenCalledWith(
expect.stringContaining('Unknown environment "staging"')
);
});

it.each(['development', 'preview', 'production'])('does not warn for %s', environment => {
const ctx = createEnvironmentWarningCtx(environment);
warnOnUnknownEnvironment(ctx);
expect(ctx.markBuildPhaseHasWarnings).not.toHaveBeenCalled();
expect(ctx.logger.warn).not.toHaveBeenCalled();
});

it('does not warn when no environment is set', () => {
const ctx = createEnvironmentWarningCtx(undefined);
warnOnUnknownEnvironment(ctx);
expect(ctx.logger.warn).not.toHaveBeenCalled();
});
});

describe(build.name, () => {
it('warns during spin-up when the job environment is unknown', async () => {
const warn = jest.fn();
const markBuildPhaseHasWarnings = jest.fn();
const ctx = {
metadata: { environment: 'staging' },
job: { platform: Platform.ANDROID },
logger: { info: jest.fn(), warn, child: jest.fn() },
markBuildPhaseHasWarnings,
runBuildPhase: jest.fn(async (_phase, callback) => callback()),
} as unknown as BuildContext;
const analytics = { logEvent: jest.fn(), flushEventsAsync: jest.fn() } as unknown as Analytics;

await build({ ctx, buildId: 'build-id', analytics });

expect(Builders.androidBuilder).toHaveBeenCalled();
expect(markBuildPhaseHasWarnings).toHaveBeenCalled();
expect(warn).toHaveBeenCalledWith(expect.stringContaining('Unknown environment "staging"'));
});
});
21 changes: 21 additions & 0 deletions packages/worker/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ import { Analytics, Event, logProjectDependenciesAsync } from './external/analyt
import { prepareRuntimeEnvironment } from './runtimeEnvironment';
import { cleanUpWorkingdir } from './workingdir';

const KNOWN_BUILD_ENVIRONMENTS = ['development', 'preview', 'production'];

type EnvironmentWarningContext = {
metadata?: { environment?: string };
logger: Pick<bunyan, 'warn'>;
markBuildPhaseHasWarnings: () => void;
};

export function warnOnUnknownEnvironment(ctx: EnvironmentWarningContext): void {
const environment = ctx.metadata?.environment;
if (environment && !KNOWN_BUILD_ENVIRONMENTS.includes(environment)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We support custom environments, so specifying a not-known environment is not an issue per se as long as the user has environment variables in that environment?

Initially I thought we might color the environment pill yellow on workflow run page if the environment has no variables, but that would color it yellow if a user does not use EAS Environment Variables at all which is bad?

Maybe we do this ^ but only if environment is not one of the known environments? Or sth like that

ctx.markBuildPhaseHasWarnings();
ctx.logger.warn(
`Unknown environment "${environment}". Expected one of: ${KNOWN_BUILD_ENVIRONMENTS.join(
', '
)}. No environment variables were added for it.`
);
}
}

export async function build({
ctx,
buildId,
Expand All @@ -40,6 +60,7 @@ export async function build({
{ job: omit(ctx.job, 'secrets', 'projectArchive') },
'Builder is ready, starting build'
);
warnOnUnknownEnvironment(ctx);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

started with a new build phase but thought it was overkill and moved here. Let me know if you have any different thoughts

},
{ doNotMarkStart: true }
);
Expand Down
Loading