Repository: codecov/uploader
Pull request #920 (2023-01-25 20:04:41)

feat: golang report fixing

codecov[bot]: # [Codecov](https://app.codecov.io/gh/codecov/uploader/compare/920?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov) Report
> Merging [#920](https://app.codecov.io/gh/codecov/uploader/compare/920?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov) (cafb6f0) into [main](https://codecov.io/gh/codecov/uploader/commit/bc4b55a694478ac480a1fda4ed030c164c0c5438?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov) (bc4b55a) will **increase** coverage by `0.03%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##             main     #920      +/-   ##
==========================================
+ Coverage   92.26%   92.29%   +0.03%     
==========================================
  Files          35       35              
  Lines        1228     1233       +5     
  Branches      251      252       +1     
==========================================
+ Hits         1133     1138       +5     
  Misses         63       63              
  Partials       32       32              
```

| Flag | Coverage Δ | |
|---|---|---|
| alpine | `92.21% <100.00%> (+0.03%)` | :arrow_up: |
| alpine-proxy | `92.21% <100.00%> (+0.03%)` | :arrow_up: |
| alpine-without-git | `92.21% <100.00%> (+0.03%)` | :arrow_up: |
| linux | `92.21% <100.00%> (+0.03%)` | :arrow_up: |
| linux-without-git | `92.21% <100.00%> (+0.03%)` | :arrow_up: |
| macos | `92.21% <100.00%> (+0.03%)` | :arrow_up: |
| macos-without-git | `92.21% <100.00%> (+0.03%)` | :arrow_up: |
| windows | `91.56% <100.00%> (+0.03%)` | :arrow_up: |
| windows-without-git | `91.56% <100.00%> (+0.03%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://app.codecov.io/gh/codecov/uploader/compare/920?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov) | Coverage Δ | |
|---|---|---|
| [src/helpers/fixes.ts](https://app.codecov.io/gh/codecov/uploader/compare/920?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=codecov#diff-c3JjL2hlbHBlcnMvZml4ZXMudHM=) | `100.00% <100.00%> (ø)` | |


Patch:
diff --git a/src/helpers/fixes.ts b/src/helpers/fixes.ts
index 2124465..c9e3315 100644
--- a/src/helpers/fixes.ts
+++ b/src/helpers/fixes.ts
@@ -20,6 +20,12 @@ export async function generateFixes(projectRoot: string): Promise<string> {
   const SYNTAXBRACKET = /^\s*[{}]\s*(\/\/.*)?$/m
   // [ or ]
   const SYNTAXLIST = /^\s*[[\]]\s*(\/\/.*)?$/m
+  // //
+  const SYNTAXCOMMENT = /^\s*\/\/.*$/m
+  // /* or */
+  const SYNTAXBLOCK = /^\s*(\/\*|\*\/).*$/m
+  // func {
+  const SYNTAXGOFUNC = /^\s*func.*\{\s*$/mg
 
   for (const file of allFiles) {
     let lineAdjustments: string[] = []
@@ -38,6 +44,10 @@ export async function generateFixes(projectRoot: string): Promise<string> {
         file.match(/\.php$/)
     ) {
       lineAdjustments = await getMatchedLines(file, [SYNTAXBRACKET, SYNTAXLIST])
+    } else if (
+        file.match(/\.go$/)
+    ) {
+      lineAdjustments = await getMatchedLines(file, [EMPTYLINE, SYNTAXCOMMENT, SYNTAXBLOCK, SYNTAXBRACKET, SYNTAXGOFUNC])
     }
 
     if (lineAdjustments.length > 0) {


Test patch:
diff --git a/test/fixtures/fixes/example.go b/test/fixtures/fixes/example.go
new file mode 100644
index 0000000..cdb9adf
--- /dev/null
+++ b/test/fixtures/fixes/example.go
@@ -0,0 +1,34 @@
+package calculator
+
+import "errors"
+
+/* this is a comment
+of some length
+*/
+
+// another comment
+
+func Add(x, y int) (int, error) {
+	return x + y, nil
+}
+
+// comments
+
+func Subtract(x, y int) (int, error) {
+	return x - y, nil
+}
+
+func Multiply(x, y int) (int, error) {
+	return x * y, nil
+}
+
+/*
+I like to divide
+*/
+
+func Divide(x, y int) (float64, error) {
+	if y == 0 {
+		return 0, errors.New("Cannot divide by 0")
+	}
+	return float64(x) / float64(y), nil
+}
diff --git a/test/helpers/fixes.test.ts b/test/helpers/fixes.test.ts
index c98d89f..1620077 100644
--- a/test/helpers/fixes.test.ts
+++ b/test/helpers/fixes.test.ts
@@ -50,6 +50,20 @@ describe('Fixes Helpers', () => {
     ).toBe('test/fixtures/fixes/example.php:4,6,11,15,19,20\n')
   })
 
+  it('provides proper fixes for go-like files', async () => {
+    const files = ['test/fixtures/fixes/example.go']
+    td.replace(childProcess, 'spawnSync', () => {
+      return {
+        stdout: files.join('\n'),
+        status: 0,
+        error: undefined,
+      }
+    })
+    expect(
+      await fixesHelpers.generateFixes('.')
+    ).toBe('test/fixtures/fixes/example.go:2,4,5,7,8,9,10,11,13,14,15,16,17,19,20,21,23,24,25,27,28,29,32,34\n')
+  })
+
   it('provides multiple fixes for files', async () => {
     const files = ['test/fixtures/fixes/example.php', 'test/fixtures/gcov/main.c']
     td.replace(childProcess, 'spawnSync', () => {
diff --git a/test/index.test.ts b/test/index.test.ts
index 4b4630c..8a90435 100644
--- a/test/index.test.ts
+++ b/test/index.test.ts
@@ -552,7 +552,7 @@ describe('Uploader Core', () => {
       upstream: ''
     })
     expect(console.log).toHaveBeenCalledWith(
-      expect.stringMatching(/# path=fixes\ntest\/fixtures\/fixes\/example.php:4,6,11,15,19,20\ntest\/fixtures\/gcov\/main.c:2,4,11,12,13,14,16,20\n<<<<<< EOF/)
+      expect.stringMatching(/# path=fixes\ntest\/fixtures\/fixes\/example.go:2,4,5,7,8,9,10,11,13,14,15,16,17,19,20,21,23,24,25,27,28,29,32,34\ntest\/fixtures\/fixes\/example.php:4,6,11,15,19,20\ntest\/fixtures\/gcov\/main.c:2,4,11,12,13,14,16,20\n<<<<<< EOF/)
     )
   })
 })