Skip to content

Commit

Permalink
Handle MSBuild properties with versions (#24)
Browse files Browse the repository at this point in the history
Handle versions being present in MSBuild properties which could create merge conflicts.
  • Loading branch information
martincostello committed Aug 11, 2023
1 parent 10f240d commit 3aa1161
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rebaser",
"version": "1.1.0",
"version": "1.1.1",
"private": true,
"description": "A GitHub Action that attempts to rebase the current branch of a Git repository.",
"main": "lib/main.js",
Expand Down
14 changes: 12 additions & 2 deletions src/VersionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,22 @@ function tryParseVersionFromXml(value: string): Dependency | null {
const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@' });
const fragment = parser.parse(value);
const element = fragment?.PackageVersion || fragment?.PackageReference;
const version = element?.['@Version'];
let version = element?.['@Version'];
let name: string | null = null;
if (version && element) {
name = element['@Include'];
} else if (fragment) {
const keys = Object.keys(fragment);
if (keys.length === 1) {
name = keys[0];
version = fragment[name];
}
}
if (version) {
const packageVersion = NuGetVersion.tryParse(version);
if (packageVersion) {
return {
name: element?.['@Include'],
name: name || '',
version: packageVersion,
};
}
Expand Down
12 changes: 12 additions & 0 deletions tests/__snapshots__/main.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ exports[`rebaser when C# project file has conflicts matches the snapshot 1`] = `
"
`;

exports[`rebaser when Directory.Packages.props has conflicts from an MSBuild property matches the snapshot 1`] = `
"<Project>
<PropertyGroup>
<DotNetVersion>8.0.0-preview.6.23329.7</DotNetVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="System.Text.Json" Version="$(DotNetVersion)" />
</ItemGroup>
</Project>
"
`;

exports[`rebaser when Directory.Packages.props has conflicts matches the snapshot 1`] = `
"<Project>
<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/MSBuildProperty/base/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>
<PropertyGroup>
<DotNetVersion>7.0.0</DotNetVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="System.Text.Json" Version="$(DotNetVersion)" />
</ItemGroup>
</Project>
8 changes: 8 additions & 0 deletions tests/fixtures/MSBuildProperty/patch/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>
<PropertyGroup>
<DotNetVersion>7.0.2</DotNetVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="System.Text.Json" Version="$(DotNetVersion)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>
<PropertyGroup>
<DotNetVersion>8.0.0-preview.6.23329.7</DotNetVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="System.Text.Json" Version="$(DotNetVersion)" />
</ItemGroup>
</Project>
29 changes: 29 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ describe('rebaser', () => {
});
});

describe('when Directory.Packages.props has conflicts from an MSBuild property', () => {
let fixture: ActionFixture;

beforeAll(async () => {
fixture = await runFixture('MSBuildProperty');
}, rebaseTimeout);

afterAll(async () => {
await fixture?.destroy();
});

test('generates no errors', () => {
expect(core.error).toHaveBeenCalledTimes(0);
expect(core.setFailed).toHaveBeenCalledTimes(0);
});

test('outputs the correct result', () => {
expect(fixture.getOutput('result')).toBe('success');
});

test('rebases the branch', async () => {
expect(await fixture.commitHistory(3)).toEqual(['Apply target', 'Apply patch', 'Apply base']);
});

test('matches the snapshot', async () => {
expect(await fixture.getFileContent('Directory.Packages.props')).toMatchSnapshot();
});
});

describe('when package.json has conflicts', () => {
let fixture: ActionFixture;

Expand Down

0 comments on commit 3aa1161

Please sign in to comment.