Skip to content

Commit

Permalink
[affected][bugfix]: do not attempt to get hash-object on git deleted …
Browse files Browse the repository at this point in the history
…files

[affected][bugfix]: do not attempt to get hash-object on git deleted files
  • Loading branch information
leblancmeneses authored Dec 26, 2024
1 parent 9ac2700 commit fb21e88
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 32 deletions.
29 changes: 26 additions & 3 deletions apps/affected/src/affected.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ jest.mock('child_process', () => {
execSync: jest.fn(),
};
});
jest.mock('fs', () => {
const originalModule = jest.requireActual('fs');
return {
...originalModule,
existsSync: jest.fn(),
};
});
import { run } from "./main";
import * as changedFilesModule from './changedFiles';
import * as core from "@actions/core";
import * as cp from 'child_process';
import * as fs from 'fs';
import crypto from 'crypto';
import * as github from '@actions/github';

Expand Down Expand Up @@ -53,6 +61,11 @@ describe("affected.spec", () => {
return "";
});

jest.spyOn(fs, 'existsSync')
.mockImplementation((command: string) => {
return !command.includes('deleted');
});

jest.spyOn(core, "setOutput").mockImplementation(jest.fn());

const files = [
Expand Down Expand Up @@ -151,11 +164,12 @@ describe("affected.spec", () => {
describe('recommended_imagetags', () => {
const files = [
"project-api/file1.ts",
"project-api/deleted.ts",
"project-ui/file1.ts",
];

function getHash(folder: string) {
const matchedFiles = [...files.filter(f => f.startsWith(folder))].sort();
const matchedFiles = [...files.filter(f => f.startsWith(folder))].filter(x=>!x.includes('deleted')).sort();
return crypto.createHash('sha1')
.update(matchedFiles.join('\n') + '\n')
.digest('hex');
Expand All @@ -165,10 +179,15 @@ describe("affected.spec", () => {
jest.spyOn(core, "setOutput").mockImplementation(jest.fn());

const execSyncResponses = {
'git diff --name-status HEAD~1 HEAD': () => files.map(f => `M\t${f}`).join('\n'),
'git diff --name-status HEAD~1 HEAD': () => files.map(f => `${f.includes('deleted')? 'D':'M'}\t${f}`).join('\n'),
'git ls-files': () => files.join('\n'),
};

jest.spyOn(fs, 'existsSync')
.mockImplementation((command: string) => {
return !command.includes('deleted');
});

jest.spyOn(cp, 'execSync')
.mockImplementation((command: string) => {
if (command.startsWith('git hash-object')) {
Expand All @@ -177,11 +196,15 @@ describe("affected.spec", () => {
throw new Error(`Unexpected command: ${command}`);
}

if(match[1].includes('deleted')) {
throw new Error('Deleted files should not be part of the hash-object');
}

return match[1];
}

if (command.startsWith('git diff --name-status')) {
return files.map(f => `M\t${f}`).join('\n');
return files.map(f => `${f.includes('deleted')? 'D':'M'}\t${f}`).join('\n');
}

if (execSyncResponses[command]) {
Expand Down
4 changes: 3 additions & 1 deletion apps/affected/src/evaluateStatementsForHashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import picomatch from 'picomatch';
import crypto from 'crypto';
import { AST, Expression } from './parser.types';
import { EXEC_SYNC_MAX_BUFFER } from './constants';
import { existsSync } from 'fs';

interface EvaluationResult {
matchedFiles: string[];
excludedFiles: string[];
}

export function allGitFiles() {
return execSync('git ls-files', { encoding: 'utf-8', maxBuffer: EXEC_SYNC_MAX_BUFFER }).split('\n').filter(Boolean);
return execSync('git ls-files', { encoding: 'utf-8', maxBuffer: EXEC_SYNC_MAX_BUFFER }).split('\n').filter(Boolean)
.filter(file => existsSync(file));
};

export async function evaluateStatementsForHashes(statements: AST, allFiles: string[]): Promise<Record<string, string>> {
Expand Down
Loading

0 comments on commit fb21e88

Please sign in to comment.