Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check renamed change files #580

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions change/beachball-fdbfaaed-1a31-4644-a756-9b44db0b66d1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "check for renamed change files",
"packageName": "beachball",
"email": "[email protected]",
"dependentChangeType": "patch"
}
35 changes: 35 additions & 0 deletions src/__tests__/validation/areChangeFilesDeleted.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { areChangeFilesDeleted } from '../../validation/areChangeFilesDeleted';
import { BeachballOptions } from '../../types/BeachballOptions';
import { findProjectRoot, getChangePath } from '../../paths';
import { getChangesBetweenRefs } from 'workspace-tools';

jest.mock('workspace-tools',() => ({
getChangesBetweenRefs: jest.fn().mockReturnValue(['file1.json', 'file2.json'])
}));

jest.mock('../../paths', () => ({
findProjectRoot: jest.fn().mockReturnValue('/'),
getChangePath: jest.fn().mockReturnValue('/change')
}));

const options = {
branch: 'test',
cwd: '/'
} as unknown as BeachballOptions;

test('return true when change files have been deleted or renamed', () => {
const changeFilesDeleted = areChangeFilesDeleted(options);
expect(getChangesBetweenRefs).toBeCalled();
expect(findProjectRoot).toBeCalled();
expect(getChangePath).toBeCalled();
expect(changeFilesDeleted).toBe(true);
});

test('return false when change files have not been deleted or renamed', () => {
(getChangesBetweenRefs as jest.Mock).mockReturnValue([]);
const changeFilesDeleted = areChangeFilesDeleted(options);
expect(getChangesBetweenRefs).toBeCalled();
expect(findProjectRoot).toBeCalled();
expect(getChangePath).toBeCalled();
expect(changeFilesDeleted).toBe(false);
});
6 changes: 3 additions & 3 deletions src/validation/areChangeFilesDeleted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export function areChangeFilesDeleted(options: BeachballOptions): boolean {
process.exit(1);
}

console.log(`Checking for deleted change files against "${branch}"`);
console.log(`Checking for deleted and renamed change files against "${branch}"`);
Copy link
Member

Choose a reason for hiding this comment

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

I think it would make sense to rename the function too: areChangeFilesDeletedOrRenamed. A bit on verbose side, but reflects the purpose of the function :)

Copy link
Author

Choose a reason for hiding this comment

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

I suppose that would be more descriptive. However, it will break the API. There are a couple of options I can see to avoid this:

  1. Create a new more descriptive function while keeping the current one and marking it for deprecation in the future.
  2. Create a new method that just checks for renamed change files and calling it based on a flag like --disallow-renamed-change-files which can be used in conjunction with the current function.
    Which option should we go with - or are there other options?

Copy link
Member

Choose a reason for hiding this comment

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

I think option #2 makes more sense :)

Copy link
Member

@ecraig12345 ecraig12345 Sep 9, 2021

Choose a reason for hiding this comment

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

@LarryKiniu This is an internal API, so you don't need to worry about breaking it. (I'm fine with either renaming the function or just going with this as-is.)

const changeFilesDeletedSinceRef = getChangesBetweenRefs(
branch,
'HEAD',
[
'--diff-filter=D', // showing only deleted files from the diff.
'--diff-filter=DR', // showing only deleted and renamed change files from the diff.
LarryKiniu marked this conversation as resolved.
Show resolved Hide resolved
],
`${changePath}/*.json`,
root
Expand All @@ -37,7 +37,7 @@ export function areChangeFilesDeleted(options: BeachballOptions): boolean {

if (changeFilesDeleted) {
const changeFiles = changeFilesDeletedSinceRef.map(file => `- ${file}`);
const errorMessage = 'The following change files were deleted:';
const errorMessage = 'The following change files were deleted or renamed:';

console.error(`${errorMessage}\n${changeFiles.join('\n')}\n`);
}
Expand Down