[tests] fix flakey tests (#9071)

Fixes flakey tests / CI:

- git metadata test for corrupted `.git` directory
- version identifier for `build-utils` being using in `fs-detectors`'s `devDependencies`
- bad import from `../dist/..`
This commit is contained in:
Sean Massa
2022-12-12 22:19:26 -06:00
committed by GitHub
parent 142c2cede0
commit 25e2b7f0ce
6 changed files with 38 additions and 38 deletions

View File

@@ -20,7 +20,7 @@ concurrency:
jobs: jobs:
test: test:
name: Unit name: Unit
timeout-minutes: 15 timeout-minutes: 20
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:

View File

@@ -5,7 +5,7 @@ import git from 'git-last-commit';
import { exec } from 'child_process'; import { exec } from 'child_process';
import { GitMetadata, Project } from '../types'; import { GitMetadata, Project } from '../types';
import { Output } from './output'; import { Output } from './output';
import { errorToString } from '@vercel/error-utils'; import { errorToString, normalizeError } from '@vercel/error-utils';
export async function createGitMeta( export async function createGitMeta(
directory: string, directory: string,
@@ -40,20 +40,28 @@ export async function createGitMeta(
return; return;
} }
const [commit, dirty] = await Promise.all([ const [commitResult, dirtyResult] = await Promise.allSettled([
getLastCommit(directory).catch(err => { getLastCommit(directory),
output.debug( isDirty(directory),
`Failed to get last commit. The directory is likely not a Git repo, there are no latest commits, or it is corrupted.\n${err}`
);
return;
}),
isDirty(directory, output),
]); ]);
if (!commit) { if (commitResult.status === 'rejected') {
output.debug(
`Failed to get last commit. The directory is likely not a Git repo, there are no latest commits, or it is corrupted.\n${commitResult.reason}`
);
return; return;
} }
if (dirtyResult.status === 'rejected') {
output.debug(
`Failed to determine if Git repo has been modified:\n${dirtyResult.reason}`
);
return;
}
const dirty = dirtyResult.value;
const commit = commitResult.value;
return { return {
remoteUrl, remoteUrl,
commitAuthorName: commit.author.name, commitAuthorName: commit.author.name,
@@ -68,7 +76,10 @@ function getLastCommit(directory: string): Promise<git.Commit> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
git.getLastCommit( git.getLastCommit(
(err, commit) => { (err, commit) => {
if (err) return reject(err); if (err) {
return reject(normalizeError(err));
}
resolve(commit); resolve(commit);
}, },
{ dst: directory } { dst: directory }
@@ -76,8 +87,8 @@ function getLastCommit(directory: string): Promise<git.Commit> {
}); });
} }
export function isDirty(directory: string, output: Output): Promise<boolean> { export function isDirty(directory: string): Promise<boolean> {
return new Promise(resolve => { return new Promise((resolve, reject) => {
// note: we specify the `--no-optional-locks` git flag so that `git status` // note: we specify the `--no-optional-locks` git flag so that `git status`
// does not perform any "optional" operations such as optimizing the index // does not perform any "optional" operations such as optimizing the index
// in the background: https://git-scm.com/docs/git-status#_background_refresh // in the background: https://git-scm.com/docs/git-status#_background_refresh
@@ -85,14 +96,15 @@ export function isDirty(directory: string, output: Output): Promise<boolean> {
'git --no-optional-locks status -s', 'git --no-optional-locks status -s',
{ cwd: directory }, { cwd: directory },
function (err, stdout, stderr) { function (err, stdout, stderr) {
let debugMessage = `Failed to determine if Git repo has been modified:`; if (err) {
stderr = stderr && stderr.trim(); return reject(err);
if (err || stderr) {
if (err) debugMessage += `\n${err}`;
if (stderr) debugMessage += `\n${stderr.trim()}`;
output.debug(debugMessage);
return resolve(false);
} }
if (stderr !== undefined && stderr.trim().length > 0) {
return reject(new Error(stderr));
}
// Example output (when dirty):
// M ../fs-detectors/src/index.ts
resolve(stdout.trim().length > 0); resolve(stdout.trim().length > 0);
} }
); );

View File

@@ -174,7 +174,7 @@ describe('createGitMeta', () => {
const directory = fixture('dirty'); const directory = fixture('dirty');
try { try {
await fs.rename(join(directory, 'git'), join(directory, '.git')); await fs.rename(join(directory, 'git'), join(directory, '.git'));
const dirty = await isDirty(directory, client.output); const dirty = await isDirty(directory);
expect(dirty).toBeTruthy(); expect(dirty).toBeTruthy();
} finally { } finally {
await fs.rename(join(directory, '.git'), join(directory, 'git')); await fs.rename(join(directory, '.git'), join(directory, 'git'));
@@ -184,7 +184,7 @@ describe('createGitMeta', () => {
const directory = fixture('not-dirty'); const directory = fixture('not-dirty');
try { try {
await fs.rename(join(directory, 'git'), join(directory, '.git')); await fs.rename(join(directory, 'git'), join(directory, '.git'));
const dirty = await isDirty(directory, client.output); const dirty = await isDirty(directory);
expect(dirty).toBeFalsy(); expect(dirty).toBeFalsy();
} finally { } finally {
await fs.rename(join(directory, '.git'), join(directory, 'git')); await fs.rename(join(directory, '.git'), join(directory, 'git'));
@@ -277,13 +277,6 @@ describe('createGitMeta', () => {
`Failed to get last commit. The directory is likely not a Git repo, there are no latest commits, or it is corrupted.` `Failed to get last commit. The directory is likely not a Git repo, there are no latest commits, or it is corrupted.`
); );
// skip next line
await lines.next();
line = await lines.next();
expect(line.value).toContain(
`Failed to determine if Git repo has been modified:`
);
expect(data).toBeUndefined(); expect(data).toBeUndefined();
} finally { } finally {
await fs.remove(tmpDir); await fs.remove(tmpDir);

View File

@@ -35,7 +35,7 @@
"@types/minimatch": "3.0.5", "@types/minimatch": "3.0.5",
"@types/node": "14.18.33", "@types/node": "14.18.33",
"@types/semver": "7.3.10", "@types/semver": "7.3.10",
"@vercel/build-utils": "4.2.0", "@vercel/build-utils": "5.7.0",
"typescript": "4.3.4" "typescript": "4.3.4"
} }
} }

View File

@@ -1,7 +1,7 @@
import { join } from 'path'; import { join } from 'path';
import { promises as fs } from 'fs'; import { promises as fs } from 'fs';
import { BuildResultV2, Meta } from '../../../build-utils/dist'; import { BuildResultV2, Meta } from '@vercel/build-utils';
import { Framework } from '../../../frameworks/dist/types'; import { Framework } from '@vercel/frameworks';
const BUILD_OUTPUT_DIR = '.vercel/output'; const BUILD_OUTPUT_DIR = '.vercel/output';

View File

@@ -3384,11 +3384,6 @@
"@typescript-eslint/types" "5.21.0" "@typescript-eslint/types" "5.21.0"
eslint-visitor-keys "^3.0.0" eslint-visitor-keys "^3.0.0"
"@vercel/build-utils@4.2.0":
version "4.2.0"
resolved "https://registry.yarnpkg.com/@vercel/build-utils/-/build-utils-4.2.0.tgz#251a496bd426f06ed3e882047728e07121c1d303"
integrity sha512-W0TKBUGaUXHj16thnPjzFUT94rR6Hkw0Zf+PlZbUN2Op7KswgBCl5At306tz5xi/BYC8lXc9WqDd3ZWnfiw4Ng==
"@vercel/fun@1.0.4": "@vercel/fun@1.0.4":
version "1.0.4" version "1.0.4"
resolved "https://registry.yarnpkg.com/@vercel/fun/-/fun-1.0.4.tgz#5d01cca962488b2328063b0c3fc0b6fac2555c2a" resolved "https://registry.yarnpkg.com/@vercel/fun/-/fun-1.0.4.tgz#5d01cca962488b2328063b0c3fc0b6fac2555c2a"