git » summer » commit 5dc4f59

Implement dry-run mode (-n)

author Alberto Bertogli
2023-04-05 07:01:56 UTC
committer Alberto Bertogli
2023-04-05 07:01:56 UTC
parent a03d8f9b2ae47c620ababbf5b3c2fcdd34a036df

Implement dry-run mode (-n)

db.go +12 -0
test/dryrun_sqlite.t +42 -0
test/dryrun_xattr.t +42 -0
test/help.t +3 -0

diff --git a/db.go b/db.go
index c26ea07..a186096 100644
--- a/db.go
+++ b/db.go
@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"database/sql"
 	"encoding/binary"
+	"flag"
 	"os"
 	"path/filepath"
 
@@ -12,6 +13,9 @@ import (
 	_ "github.com/mattn/go-sqlite3"
 )
 
+var dryRun = flag.Bool("n", false,
+	"dry-run mode (do not write anything)")
+
 type DB interface {
 	Has(f *os.File) (bool, error)
 	Read(f *os.File) (ChecksumV1, error)
@@ -44,6 +48,10 @@ func (_ XattrDB) Read(f *os.File) (ChecksumV1, error) {
 }
 
 func (_ XattrDB) Write(f *os.File, cs ChecksumV1) error {
+	if *dryRun {
+		return nil
+	}
+
 	buf := new(bytes.Buffer)
 	err := binary.Write(buf, binary.LittleEndian, cs)
 	if err != nil {
@@ -110,6 +118,10 @@ func (s *SqliteDB) Read(f *os.File) (ChecksumV1, error) {
 }
 
 func (s *SqliteDB) Write(f *os.File, cs ChecksumV1) error {
+	if *dryRun {
+		return nil
+	}
+
 	path, err := filepath.Rel(s.root, f.Name())
 	if err != nil {
 		return err
diff --git a/test/dryrun_sqlite.t b/test/dryrun_sqlite.t
new file mode 100644
index 0000000..63dfbbe
--- /dev/null
+++ b/test/dryrun_sqlite.t
@@ -0,0 +1,42 @@
+Tests for dry-run mode when using -db. Similar to the basic ones.
+
+  $ alias summer="$TESTDIR/../summer"
+
+Generate test data.
+
+  $ touch empty
+  $ echo marola > hola
+
+Generate and verify.
+
+  $ summer -n -db=db.sqlite3 generate .
+  \r (no-eol) (esc)
+  0s: 0 matched, 0 modified, 3 new, 0 corrupted
+
+  $ summer -n -db=db.sqlite3 verify .
+  \r (no-eol) (esc)
+  0s: 0 matched, 0 modified, 3 new, 0 corrupted
+
+  $ summer -n -db=db.sqlite3 verify .
+  \r (no-eol) (esc)
+  0s: 0 matched, 0 modified, 3 new, 0 corrupted
+
+Now write data for real, so we can test modification.
+
+  $ summer -db=db.sqlite3 generate .
+  \r (no-eol) (esc)
+  0s: 0 matched, 0 modified, 3 new, 0 corrupted
+
+Check handling of new and updated files.
+
+  $ echo trova > nueva
+  $ touch empty
+  $ summer -n -db=db.sqlite3 verify .
+  \r (no-eol) (esc)
+  0s: 1 matched, 2 modified, 1 new, 0 corrupted
+  $ summer -n -db=db.sqlite3 update .
+  \r (no-eol) (esc)
+  0s: 1 matched, 2 modified, 1 new, 0 corrupted
+  $ summer -n -db=db.sqlite3 verify .
+  \r (no-eol) (esc)
+  0s: 1 matched, 2 modified, 1 new, 0 corrupted
diff --git a/test/dryrun_xattr.t b/test/dryrun_xattr.t
new file mode 100644
index 0000000..30a42ed
--- /dev/null
+++ b/test/dryrun_xattr.t
@@ -0,0 +1,42 @@
+Tests for dry-run mode. Similar to the basic ones.
+
+  $ alias summer="$TESTDIR/../summer"
+
+Generate test data.
+
+  $ touch empty
+  $ echo marola > hola
+
+Generate and verify.
+
+  $ summer -n generate .
+  \r (no-eol) (esc)
+  0s: 0 matched, 0 modified, 2 new, 0 corrupted
+
+  $ summer -n verify .
+  \r (no-eol) (esc)
+  0s: 0 matched, 0 modified, 2 new, 0 corrupted
+
+  $ summer -n verify .
+  \r (no-eol) (esc)
+  0s: 0 matched, 0 modified, 2 new, 0 corrupted
+
+Now write data for real, so we can test modification.
+
+  $ summer generate .
+  \r (no-eol) (esc)
+  0s: 0 matched, 0 modified, 2 new, 0 corrupted
+
+Check handling of new and updated files.
+
+  $ echo trova > nueva
+  $ touch empty
+  $ summer -n verify .
+  \r (no-eol) (esc)
+  0s: 1 matched, 1 modified, 1 new, 0 corrupted
+  $ summer -n update .
+  \r (no-eol) (esc)
+  0s: 1 matched, 1 modified, 1 new, 0 corrupted
+  $ summer -n verify .
+  \r (no-eol) (esc)
+  0s: 1 matched, 1 modified, 1 new, 0 corrupted
diff --git a/test/help.t b/test/help.t
index cd660c5..beaefd4 100644
--- a/test/help.t
+++ b/test/help.t
@@ -28,6 +28,7 @@ No arguments.
   Flags:
     -db string
       \tdatabase to read from/write to (esc)
+    -n\tdry-run mode (do not write anything) (esc)
     -q\tquiet mode (esc)
     -v\tverbose mode (list each file) (esc)
     -x\tdon't cross filesystem boundaries (esc)
@@ -60,6 +61,7 @@ Too few arguments.
   Flags:
     -db string
       \tdatabase to read from/write to (esc)
+    -n\tdry-run mode (do not write anything) (esc)
     -q\tquiet mode (esc)
     -v\tverbose mode (list each file) (esc)
     -x\tdon't cross filesystem boundaries (esc)
@@ -92,6 +94,7 @@ No valid path (the argument is given, but it is empty).
   Flags:
     -db string
       \tdatabase to read from/write to (esc)
+    -n\tdry-run mode (do not write anything) (esc)
     -q\tquiet mode (esc)
     -v\tverbose mode (list each file) (esc)
     -x\tdon't cross filesystem boundaries (esc)