Skip to content

Commit

Permalink
Fix restoring of Autofix backup (#18)
Browse files Browse the repository at this point in the history
* Fix restoring of backup

Signed-off-by: Akshit Garg <[email protected]>

* Bump version

Signed-off-by: Akshit Garg <[email protected]>

---------

Signed-off-by: Akshit Garg <[email protected]>
  • Loading branch information
akshit-deepsource authored Feb 17, 2023
1 parent 0327832 commit 2a9c9b8
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 3 deletions.
2 changes: 0 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ archives:
386: i386
amd64: x86_64
format: binary
builds_info:
mode: 0777
checksum:
name_template: 'checksums.txt'
snapshot:
Expand Down
2 changes: 1 addition & 1 deletion cmd/scatr/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
)

const version = "0.3.3"
const version = "0.3.4"

var versionCmd = &cobra.Command{
Use: "version",
Expand Down
32 changes: 32 additions & 0 deletions runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runner

import (
"errors"
"fmt"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -100,6 +101,37 @@ func testAutofix(
return nil, nil, false, err
}

diff, identical, passed, err := runAutofixTests(config, autofixDir, backup)
if err != nil {
log.Println("Autofix run error:", err)
restoreErr := restoreBackup(backup)
if restoreErr != nil {
return nil, nil, false,
fmt.Errorf("autofix err: %s, restore err: %s", err.Error(), restoreErr.Error())
}

return nil, nil, false, err
}

return diff, identical, passed, restoreBackup(backup)
}

func restoreBackup(backup *AutofixBackup) error {
log.Println("Restoring the Autofix backup")
err := backup.RestoreAndDestroy()
if err != nil {
log.Println("Unable to restore Autofix backup, err:", err)
return err
}

return nil
}

func runAutofixTests(
config *Config,
autofixDir string,
backup *AutofixBackup,
) (autofixDiff, identicalGoldenFiles, bool, error) {
log.Println("Checking for identical original and golden files")
identical, passed, err := checkIdenticalGoldenFile(config.CodePath, config.ExcludedDirs, backup)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions runner/testdata/autofix/go_failing_code_path/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package code

var foo int = 10

func bar() {
a := 10
a = a
}

func baz() {
a := 10
a = a
}

func lorem() {
_ = 10
}
16 changes: 16 additions & 0 deletions runner/testdata/autofix/go_restore/.scatr.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
files = "*.go"
comment_prefix = ["//"]

[autofix]
script = """
cat main.go.golden > main.go
exit 0
"""
interpreter = "sh"
output_file = "analysis_result.json"

[processor]
skip_processing = false
script = """
cat $INPUT_FILE
"""
1 change: 1 addition & 0 deletions runner/testdata/autofix/go_restore/files.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
1 change: 1 addition & 0 deletions runner/testdata/autofix/go_restore/files_failing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
1 change: 1 addition & 0 deletions runner/testdata/autofix/go_restore/files_identical.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["main.go"]
3 changes: 3 additions & 0 deletions runner/testdata/autofix/go_restore/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/deepsourcelabs/SCATR/testdata/autofix/go_restore

go 1.19
18 changes: 18 additions & 0 deletions runner/testdata/autofix/go_restore/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

var foo int = 10

func bar() {
a := 10
a = a
}

func baz() {
a := 10
a = a
}

func lorem() {
a := 10
a = a
}
16 changes: 16 additions & 0 deletions runner/testdata/autofix/go_restore/main.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

func bar() {
a := 10
a = a
}

func baz() {
a := 10
a = a
}

func lorem() {
a := 10
a = a
}

0 comments on commit 2a9c9b8

Please sign in to comment.