aboutsummaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
authorSimon Frei <freisim93@gmail.com>2021-03-03 08:53:50 +0100
committerGitHub <noreply@github.com>2021-03-03 08:53:50 +0100
commit3d91f7c975a862be418afa43f5ca366f6e1dbd2b (patch)
treece415ddd1c2d0e95c2d23efcd955ec769bee6679 /script
parent7945430e64d885a848887ce1a9231551067cebe7 (diff)
downloadsyncthing-3d91f7c975a862be418afa43f5ca366f6e1dbd2b.tar.gz
syncthing-3d91f7c975a862be418afa43f5ca366f6e1dbd2b.zip
lib: Use counterfeiter to mock interfaces in tests (#7375)
Diffstat (limited to 'script')
-rw-r--r--script/prune_mocks.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/script/prune_mocks.go b/script/prune_mocks.go
new file mode 100644
index 000000000..0c3705930
--- /dev/null
+++ b/script/prune_mocks.go
@@ -0,0 +1,79 @@
+// Copyright (C) 2021 The Syncthing Authors.
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this file,
+// You can obtain one at https://mozilla.org/MPL/2.0/.
+
+// +build ignore
+
+package main
+
+import (
+ "bufio"
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strings"
+)
+
+func main() {
+ var path string
+ flag.StringVar(&path, "t", "", "Name of file to prune")
+ flag.Parse()
+
+ filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
+ if err != nil {
+ log.Fatal(err)
+ }
+ if !info.Mode().IsRegular() {
+ return nil
+ }
+ err = pruneInterfaceCheck(path, info.Size())
+ if err != nil {
+ log.Fatal(err)
+ }
+ err = exec.Command("goimports", "-w", path).Run()
+ if err != nil {
+ log.Fatal(err)
+ }
+ return nil
+ })
+}
+
+func pruneInterfaceCheck(path string, size int64) error {
+ fd, err := os.Open(path)
+ if err != nil {
+ return err
+ }
+ defer fd.Close()
+
+ tmp, err := ioutil.TempFile(".", "")
+ if err != nil {
+ return err
+ }
+
+ scanner := bufio.NewScanner(fd)
+
+ for scanner.Scan() {
+ line := scanner.Text()
+ if strings.HasPrefix(strings.TrimSpace(line), "var _ ") {
+ continue
+ }
+ if _, err := tmp.WriteString(line + "\n"); err != nil {
+ os.Remove(tmp.Name())
+ return err
+ }
+ }
+
+ if err := fd.Close(); err != nil {
+ return err
+ }
+ if err := os.Remove(path); err != nil {
+ return err
+ }
+ return os.Rename(tmp.Name(), path)
+}