-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create fixes on c-like and php files (#869)
* feat: create fixes on c-like and php files * fix: add e2e test for fixes
- Loading branch information
1 parent
d73502c
commit 29b240c
Showing
7 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,3 +73,5 @@ codecov.exe | |
.coverage | ||
# File genrated as part of XCode tests | ||
coverage-report-test.json | ||
|
||
*.coverage.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import fs from 'fs' | ||
import readline from 'readline' | ||
|
||
import { getAllFiles } from './files' | ||
import { UploadLogger } from './logger' | ||
|
||
export const FIXES_HEADER = '# path=fixes\n' | ||
|
||
export async function generateFixes(projectRoot: string): Promise<string> { | ||
// Fake out the UploaderArgs as they are not needed | ||
const allFiles = await getAllFiles(projectRoot, projectRoot, { | ||
flags: '', | ||
slug: '', | ||
upstream: '', | ||
}) | ||
|
||
const allAdjustments: string[] = [] | ||
const EMPTYLINE = /^\s*$/mg | ||
// { or } | ||
const SYNTAXBRACKET = /^\s*[{}]\s*(\/\/.*)?$/m | ||
// [ or ] | ||
const SYNTAXLIST = /^\s*[[\]]\s*(\/\/.*)?$/m | ||
|
||
for (const file of allFiles) { | ||
let lineAdjustments: string[] = [] | ||
|
||
if ( | ||
file.match(/\.c$/) || | ||
file.match(/\.cpp$/) || | ||
file.match(/\.h$/) || | ||
file.match(/\.hpp$/) || | ||
file.match(/\.m$/) || | ||
file.match(/\.swift$/) || | ||
file.match(/\.vala$/) | ||
) { | ||
lineAdjustments = await getMatchedLines(file, [EMPTYLINE, SYNTAXBRACKET]) | ||
} else if ( | ||
file.match(/\.php$/) | ||
) { | ||
lineAdjustments = await getMatchedLines(file, [SYNTAXBRACKET, SYNTAXLIST]) | ||
} | ||
|
||
if (lineAdjustments.length > 0) { | ||
UploadLogger.verbose(`Matched file ${file} for adjustments: ${lineAdjustments.join(',')}`) | ||
allAdjustments.push(`${file}:${lineAdjustments.join(',')}\n`) | ||
} | ||
} | ||
return allAdjustments.join('') | ||
} | ||
|
||
async function getMatchedLines(file: string, matchers: RegExp[]): Promise<string[]> { | ||
const fileStream = fs.createReadStream(file) | ||
const rl = readline.createInterface({ | ||
input: fileStream, | ||
crlfDelay: Infinity | ||
}); | ||
|
||
const matchedLines: string[] = [] | ||
let lineNumber = 1 | ||
|
||
for await (const line of rl) { | ||
for (const matcher of matchers) { | ||
if (line.match(matcher)) { | ||
matchedLines.push(lineNumber.toString()) | ||
} | ||
} | ||
lineNumber++ | ||
} | ||
return matchedLines | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php namespace Example; | ||
|
||
class Example | ||
{ | ||
public static function go() | ||
{ | ||
$test_array = [ | ||
1, | ||
2, | ||
3, | ||
] | ||
|
||
if (false) { | ||
return true; | ||
} | ||
|
||
return false; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import td from 'testdouble' | ||
import childProcess from 'child_process' | ||
|
||
import * as fixesHelpers from '../../src/helpers/fixes' | ||
|
||
describe('Fixes Helpers', () => { | ||
afterEach(() => { | ||
td.reset() | ||
}) | ||
|
||
it('provides no fixes if none are applicable', async () => { | ||
const files = ['package.json'] | ||
td.replace(childProcess, 'spawnSync', () => { | ||
return { | ||
stdout: files.join('\n'), | ||
status: 0, | ||
error: undefined, | ||
} | ||
}) | ||
expect( | ||
await fixesHelpers.generateFixes('.') | ||
).toBe('') | ||
}) | ||
|
||
it('provides proper fixes for c-like files', async () => { | ||
const files = ['test/fixtures/gcov/main.c'] | ||
td.replace(childProcess, 'spawnSync', () => { | ||
return { | ||
stdout: files.join('\n'), | ||
status: 0, | ||
error: undefined, | ||
} | ||
}) | ||
expect( | ||
await fixesHelpers.generateFixes('.') | ||
).toBe('test/fixtures/gcov/main.c:2,4,11,12,13,14,16,20\n') | ||
}) | ||
|
||
it('provides proper fixes for php-like files', async () => { | ||
const files = ['test/fixtures/fixes/example.php'] | ||
td.replace(childProcess, 'spawnSync', () => { | ||
return { | ||
stdout: files.join('\n'), | ||
status: 0, | ||
error: undefined, | ||
} | ||
}) | ||
expect( | ||
await fixesHelpers.generateFixes('.') | ||
).toBe('test/fixtures/fixes/example.php:4,6,11,15,19,20\n') | ||
}) | ||
|
||
it('provides multiple fixes for files', async () => { | ||
const files = ['test/fixtures/fixes/example.php', 'test/fixtures/gcov/main.c'] | ||
td.replace(childProcess, 'spawnSync', () => { | ||
return { | ||
stdout: files.join('\n'), | ||
status: 0, | ||
error: undefined, | ||
} | ||
}) | ||
expect( | ||
await fixesHelpers.generateFixes('.') | ||
).toBe('test/fixtures/fixes/example.php:4,6,11,15,19,20\ntest/fixtures/gcov/main.c:2,4,11,12,13,14,16,20\n') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters