From 2a9c9b84148cd194dcaa7575efe054f2f3a62d16 Mon Sep 17 00:00:00 2001 From: Akshit Garg <107033189+akshit-deepsource@users.noreply.github.com> Date: Fri, 17 Feb 2023 11:55:12 +0530 Subject: [PATCH] Fix restoring of Autofix backup (#18) * Fix restoring of backup Signed-off-by: Akshit Garg * Bump version Signed-off-by: Akshit Garg --------- Signed-off-by: Akshit Garg --- .goreleaser.yaml | 2 -- cmd/scatr/version.go | 2 +- runner/run.go | 32 +++++++++++++++++++ .../autofix/go_failing_code_path/main.go | 17 ++++++++++ .../testdata/autofix/go_restore/.scatr.toml | 16 ++++++++++ runner/testdata/autofix/go_restore/files.json | 1 + .../autofix/go_restore/files_failing.json | 1 + .../autofix/go_restore/files_identical.json | 1 + runner/testdata/autofix/go_restore/go.mod | 3 ++ runner/testdata/autofix/go_restore/main.go | 18 +++++++++++ .../autofix/go_restore/main.go.golden | 16 ++++++++++ 11 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 runner/testdata/autofix/go_failing_code_path/main.go create mode 100644 runner/testdata/autofix/go_restore/.scatr.toml create mode 100644 runner/testdata/autofix/go_restore/files.json create mode 100644 runner/testdata/autofix/go_restore/files_failing.json create mode 100644 runner/testdata/autofix/go_restore/files_identical.json create mode 100644 runner/testdata/autofix/go_restore/go.mod create mode 100644 runner/testdata/autofix/go_restore/main.go create mode 100644 runner/testdata/autofix/go_restore/main.go.golden diff --git a/.goreleaser.yaml b/.goreleaser.yaml index e75e36e..dd368e0 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -22,8 +22,6 @@ archives: 386: i386 amd64: x86_64 format: binary - builds_info: - mode: 0777 checksum: name_template: 'checksums.txt' snapshot: diff --git a/cmd/scatr/version.go b/cmd/scatr/version.go index a365af4..9379e43 100644 --- a/cmd/scatr/version.go +++ b/cmd/scatr/version.go @@ -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", diff --git a/runner/run.go b/runner/run.go index 0aa80f6..cd4304e 100644 --- a/runner/run.go +++ b/runner/run.go @@ -2,6 +2,7 @@ package runner import ( "errors" + "fmt" "log" "os" "os/exec" @@ -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 { diff --git a/runner/testdata/autofix/go_failing_code_path/main.go b/runner/testdata/autofix/go_failing_code_path/main.go new file mode 100644 index 0000000..6d9f2ea --- /dev/null +++ b/runner/testdata/autofix/go_failing_code_path/main.go @@ -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 +} diff --git a/runner/testdata/autofix/go_restore/.scatr.toml b/runner/testdata/autofix/go_restore/.scatr.toml new file mode 100644 index 0000000..6013cfc --- /dev/null +++ b/runner/testdata/autofix/go_restore/.scatr.toml @@ -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 +""" diff --git a/runner/testdata/autofix/go_restore/files.json b/runner/testdata/autofix/go_restore/files.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/runner/testdata/autofix/go_restore/files.json @@ -0,0 +1 @@ +[] diff --git a/runner/testdata/autofix/go_restore/files_failing.json b/runner/testdata/autofix/go_restore/files_failing.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/runner/testdata/autofix/go_restore/files_failing.json @@ -0,0 +1 @@ +[] diff --git a/runner/testdata/autofix/go_restore/files_identical.json b/runner/testdata/autofix/go_restore/files_identical.json new file mode 100644 index 0000000..c3e5bd0 --- /dev/null +++ b/runner/testdata/autofix/go_restore/files_identical.json @@ -0,0 +1 @@ +["main.go"] \ No newline at end of file diff --git a/runner/testdata/autofix/go_restore/go.mod b/runner/testdata/autofix/go_restore/go.mod new file mode 100644 index 0000000..e36d28f --- /dev/null +++ b/runner/testdata/autofix/go_restore/go.mod @@ -0,0 +1,3 @@ +module github.com/deepsourcelabs/SCATR/testdata/autofix/go_restore + +go 1.19 diff --git a/runner/testdata/autofix/go_restore/main.go b/runner/testdata/autofix/go_restore/main.go new file mode 100644 index 0000000..269429e --- /dev/null +++ b/runner/testdata/autofix/go_restore/main.go @@ -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 +} diff --git a/runner/testdata/autofix/go_restore/main.go.golden b/runner/testdata/autofix/go_restore/main.go.golden new file mode 100644 index 0000000..57dd105 --- /dev/null +++ b/runner/testdata/autofix/go_restore/main.go.golden @@ -0,0 +1,16 @@ +package main + +func bar() { + a := 10 + a = a +} + +func baz() { + a := 10 + a = a +} + +func lorem() { + a := 10 + a = a +}