Skip to content

Commit

Permalink
fix: Fix incorrect ratchet behavior on relative source path (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
slarse authored May 10, 2021
1 parent 70d81bc commit 35c1a78
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
49 changes: 49 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,55 @@ test('runSorald can ratchet from HEAD~', async () => {
]);
});

test('runSorald can ratchet from HEAD~ with relative source path', async () => {
// Test that buildbreaker can ratchet from HEAD~ with a relative path to the
// source. The only reason we use HEAD~ is that it's very easy to setup the
// test.

// arrange
const workdir = await helpers.createTempdir();
const repo = await git.init(workdir);

// create a commit with a single violation each of rules 2164 (math on float) and 1854 (dead store)
const className = 'Main';
const filePath = path.join(workdir, `${className}.java`);
const baseMethodBody = `float a = 2;
float b = 2; // 1854: dead store
b = 3;
float c = a / b; // 2164: math on float
System.out.println(c);`;
const initialClassContent = wrapInClassInMainMethod(
className,
baseMethodBody
);
helpers.createFile(filePath, initialClassContent);
await repo.add(filePath);
await repo.commit('Initial commit');

// create a new commit with one more violation of each rule
const secondCommitMethodBody = `${baseMethodBody}
c = b / a; // 2164: math on float
System.out.println(c);
b = c; // 1854: dead store`;
const secondCommitClassContent = wrapInClassInMainMethod(
className,
secondCommitMethodBody
);
await helpers.createFile(filePath, secondCommitClassContent);
await repo.add(filePath);
await repo.commit('Update file');

// act
const soraldJarUrl =
'https://github.com/SpoonLabs/sorald/releases/download/sorald-0.1.0/sorald-0.1.0-jar-with-dependencies.jar';
const relativeSourcePath = path.relative('.', workdir.toString());
const repairs = main.runSorald(relativeSourcePath, soraldJarUrl, 'HEAD~');

await expect(repairs).resolves.toEqual([
'1854:Main.java:10:4:10:7',
'2164:Main.java:8:6:8:11'
]);
});
test('runSorald with ratchet does nothing when there are no violations in changed code', async () => {
// arrange
const workdir = await helpers.createTempdir();
Expand Down
13 changes: 7 additions & 6 deletions dist/index.js

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

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

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@ export async function runSorald(
soraldJarUrl: string,
ratchetFrom?: string
): Promise<string[]> {
const sourceAbsPath = path.resolve(source.toString());
const jarDstPath = 'sorald.jar';
const repo = new git.Repo(source);
const repo = new git.Repo(sourceAbsPath);

core.info(`Downloading Sorald jar to ${jarDstPath}`);
await download(soraldJarUrl, jarDstPath);

core.info(`Mining rule violations at ${source}`);
core.info(`Mining rule violations at ${sourceAbsPath}`);
const unfilteredKeyToSpecs: Map<number, string[]> = await sorald.mine(
jarDstPath,
source,
sourceAbsPath,
'stats.json'
);
const keyToSpecs = await filterKeyToSpecsByRatchet(
unfilteredKeyToSpecs,
source,
sourceAbsPath,
repo,
ratchetFrom
);
Expand All @@ -56,10 +57,10 @@ export async function runSorald(
core.info('Attempting repairs');
for (const [ruleKey, violationSpecs] of keyToSpecs.entries()) {
core.info(`Repairing violations of rule ${ruleKey}: ${violationSpecs}`);
const statsFile = path.join(source.toString(), `${ruleKey}.json`);
const statsFile = path.join(sourceAbsPath, `${ruleKey}.json`);
const repairs = await sorald.repair(
jarDstPath,
source,
sourceAbsPath,
statsFile,
violationSpecs
);
Expand Down

0 comments on commit 35c1a78

Please sign in to comment.