Skip to content

Commit

Permalink
Merge branch 'master' into indexdb
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak committed Dec 11, 2024
2 parents 901aaa4 + c0aee19 commit 781d23f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Flags:
-S, --[no-]skip-symlinks do not follow symlinks
-R, --[no-]no-recurse do not recurse into subdirectories
-D, --[no-]no-dir-in-index do not track directories in the index
--no-config ignore the config file
--force force update of damaged items (advanced usage
only)
-l, --log-file=STRING write to a logfile if specified
Expand Down
16 changes: 14 additions & 2 deletions cmd/chkbit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
termAlertFG = lterm.Fg4(1)
)

var cli struct {
type CLI struct {
Paths []string `arg:"" optional:"" name:"paths" help:"directories to check"`
Tips bool `short:"H" help:"Show tips."`
Check bool `short:"c" help:"chkbit will verify files in readonly mode (default mode)" xor:"mode" group:"Mode"`
Expand All @@ -56,6 +56,7 @@ var cli struct {
SkipSymlinks bool `short:"S" help:"do not follow symlinks" negatable:""`
NoRecurse bool `short:"R" help:"do not recurse into subdirectories" negatable:""`
NoDirInIndex bool `short:"D" help:"do not track directories in the index" negatable:""`
NoConfig bool `help:"ignore the config file"`
Force bool `help:"force update of damaged items (advanced usage only)"`
LogFile string `short:"l" help:"write to a logfile if specified"`
LogVerbose bool `help:"verbose logging" negatable:""`
Expand All @@ -70,6 +71,8 @@ var cli struct {
Version bool `short:"V" help:"show version information"`
}

var cli CLI

type Main struct {
context *chkbit.Context
dmgList []string
Expand Down Expand Up @@ -318,9 +321,18 @@ func (m *Main) run() int {
kong.Configuration(kong.JSON, configPath),
)

if cli.NoConfig {
cli = CLI{}
kong.Parse(&cli,
kong.Name("chkbit"),
kong.Description(headerHelp),
kong.UsageOnError(),
)
}

if cli.Tips {
fmt.Println(strings.ReplaceAll(helpTips, "<config-file>", configPath))
return 0
os.Exit(0)
}

if cli.Version {
Expand Down
38 changes: 19 additions & 19 deletions scripts/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import (

var testDir = "/tmp/chkbit"

func getCmd() string {
func runCmd(args ...string) *exec.Cmd {
_, filename, _, _ := runtime.Caller(0)
prjRoot := filepath.Dir(filepath.Dir(filename))
return filepath.Join(prjRoot, "chkbit")
tool := filepath.Join(prjRoot, "chkbit")
args = append([]string{"--no-config"}, args...)
return exec.Command(tool, args...)
}

func checkOut(t *testing.T, sout string, expected string) {
Expand Down Expand Up @@ -139,12 +141,11 @@ func setupMiscFiles() {
func TestRoot(t *testing.T) {
setupMiscFiles()

tool := getCmd()
root := filepath.Join(testDir, "root")

// update index, no recourse
t.Run("no-recourse", func(t *testing.T) {
cmd := exec.Command(tool, "-umR", filepath.Join(root, "day/office"))
cmd := runCmd("-umR", filepath.Join(root, "day/office"))
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
Expand All @@ -159,7 +160,7 @@ func TestRoot(t *testing.T) {

// update remaining index from root
t.Run("update-remaining", func(t *testing.T) {
cmd := exec.Command(tool, "-um", root)
cmd := runCmd("-um", root)
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
Expand All @@ -177,7 +178,7 @@ func TestRoot(t *testing.T) {
os.RemoveAll(filepath.Join(root, "thing/change"))
os.Remove(filepath.Join(root, "time/hour/minute/body-information.csv"))

cmd := exec.Command(tool, "-m", root)
cmd := runCmd("-m", root)
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
Expand All @@ -189,7 +190,7 @@ func TestRoot(t *testing.T) {

// do not report missing without -m
t.Run("no-missing", func(t *testing.T) {
cmd := exec.Command(tool, root)
cmd := runCmd(root)
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
Expand All @@ -201,7 +202,7 @@ func TestRoot(t *testing.T) {

// check for missing and update
t.Run("missing", func(t *testing.T) {
cmd := exec.Command(tool, "-um", root)
cmd := runCmd("-um", root)
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
Expand All @@ -214,7 +215,7 @@ func TestRoot(t *testing.T) {
// check again
t.Run("repeat", func(t *testing.T) {
for i := 0; i < 10; i++ {
cmd := exec.Command(tool, "-uv", root)
cmd := runCmd("-uv", root)
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
Expand All @@ -233,7 +234,7 @@ func TestRoot(t *testing.T) {
genFiles(filepath.Join(root, "way/add"), 99)
genFile(filepath.Join(root, "time/add-file.txt"), 500)

cmd := exec.Command(tool, "-a", root)
cmd := runCmd("-a", root)
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
Expand All @@ -251,7 +252,7 @@ func TestRoot(t *testing.T) {
// modify existing
genFile(filepath.Join(root, "way/job/word-business.mp3"), 500)

cmd := exec.Command(tool, "-a", root)
cmd := runCmd("-a", root)
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
Expand All @@ -266,7 +267,7 @@ func TestRoot(t *testing.T) {

// update remaining
t.Run("update-remaining-add", func(t *testing.T) {
cmd := exec.Command(tool, "-u", root)
cmd := runCmd("-u", root)
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
Expand All @@ -281,7 +282,7 @@ func TestRoot(t *testing.T) {
genFiles(filepath.Join(root, "way/.hidden"), 99)
genFile(filepath.Join(root, "time/.ignored"), 999)

cmd := exec.Command(tool, "-u", root)
cmd := runCmd("-u", root)
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
Expand All @@ -293,7 +294,7 @@ func TestRoot(t *testing.T) {
// include dot
t.Run("include-dot", func(t *testing.T) {

cmd := exec.Command(tool, "-u", "-d", root)
cmd := runCmd("-u", "-d", root)
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
Expand Down Expand Up @@ -323,7 +324,6 @@ func TestDMG(t *testing.T) {
panic(err)
}

tool := getCmd()
testFile := filepath.Join(testDmg, "test.txt")
t1, _ := time.Parse(time.RFC3339, "2022-02-01T11:00:00Z")
t2, _ := time.Parse(time.RFC3339, "2022-02-01T12:00:00Z")
Expand All @@ -334,7 +334,7 @@ func TestDMG(t *testing.T) {
os.WriteFile(testFile, []byte("foo1"), 0644)
os.Chtimes(testFile, t2, t2)

cmd := exec.Command(tool, "-u", ".")
cmd := runCmd("-u", ".")
if out, err := cmd.Output(); err != nil {
t.Fatalf("failed with '%s'\n", err)
} else {
Expand All @@ -347,7 +347,7 @@ func TestDMG(t *testing.T) {
os.WriteFile(testFile, []byte("foo2"), 0644)
os.Chtimes(testFile, t1, t1)

cmd := exec.Command(tool, "-u", ".")
cmd := runCmd("-u", ".")
if out, err := cmd.Output(); err != nil {
t.Fatalf("failed with '%s'\n", err)
} else {
Expand All @@ -360,7 +360,7 @@ func TestDMG(t *testing.T) {
os.WriteFile(testFile, []byte("foo3"), 0644)
os.Chtimes(testFile, t3, t3)

cmd := exec.Command(tool, "-u", ".")
cmd := runCmd("-u", ".")
if out, err := cmd.Output(); err != nil {
t.Fatalf("failed with '%s'\n", err)
} else {
Expand All @@ -373,7 +373,7 @@ func TestDMG(t *testing.T) {
os.WriteFile(testFile, []byte("foo4"), 0644)
os.Chtimes(testFile, t3, t3)

cmd := exec.Command(tool, "-u", ".")
cmd := runCmd("-u", ".")
if out, err := cmd.Output(); err != nil {
if cmd.ProcessState.ExitCode() != 1 {
t.Fatalf("expected to fail with exit code 1 vs %d!", cmd.ProcessState.ExitCode())
Expand Down
4 changes: 0 additions & 4 deletions scripts/tests
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@ echo "# prep files"
$script_dir/build

echo "# test files"
if [[ -f ~/.config/chkbit/config.json ]]; then
echo 'error: unable to test with config file preset'
exit 1
fi
go test -v ./scripts -count=1

0 comments on commit 781d23f

Please sign in to comment.