Skip to content

Commit

Permalink
Merge pull request twpayne#1232 from twpayne/refactor-dir-structure
Browse files Browse the repository at this point in the history
Tidy up tests and refactor directory structure
  • Loading branch information
twpayne committed May 25, 2021
2 parents 5258802 + 5eb6def commit 4191e4b
Show file tree
Hide file tree
Showing 170 changed files with 50 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ jobs:
with:
scandir: ./assets/scripts
- name: Whitespace
run: go run ./internal/cmd/lint-whitespace
run: go run ./internal/cmds/lint-whitespace
- name: Typos
run: |
go install github.com/twpayne/findtypos@latest
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ issues:
- linters:
- forbidigo
- gosec
path: ^internal/cmd/
path: ^internal/cmds/
- linters:
- gosec
path: "_test\\.go$"
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ completions:
$(GO) run . completion powershell -o completions/chezmoi.ps1
$(GO) run . completion zsh -o completions/chezmoi.zsh

assets/scripts/install.sh: internal/cmd/generate-install.sh/install.sh.tmpl internal/cmd/generate-install.sh/main.go
$(GO) run ./internal/cmd/generate-install.sh > $@
assets/scripts/install.sh: internal/cmds/generate-install.sh/install.sh.tmpl internal/cmds/generate-install.sh/main.go
$(GO) run ./internal/cmds/generate-install.sh > $@

.PHONY: lint
lint: ensure-golangci-lint
./bin/golangci-lint run
$(GO) run ./internal/cmd/lint-whitespace
$(GO) run ./internal/cmds/lint-whitespace

.PHONY: format
format: ensure-gofumports
Expand Down
48 changes: 24 additions & 24 deletions chezmoi.io/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,38 @@ content-docs: \
content/docs/related.md \
content/docs/templating.md

content/docs/architecture.md: ../docs/ARCHITECTURE.md ../internal/cmd/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmd/generate-chezmoi.io-content-docs -shorttitle="Architecture" -longtitle="Architecture" < $< > $@ || ( rm -f $@ ; false )
content/docs/architecture.md: ../docs/ARCHITECTURE.md ../internal/cmds/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmds/generate-chezmoi.io-content-docs -shorttitle="Architecture" -longtitle="Architecture" < $< > $@ || ( rm -f $@ ; false )

content/docs/changes.md: ../docs/CHANGES.md ../internal/cmd/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmd/generate-chezmoi.io-content-docs -shorttitle="Changes" -longtitle="Changes" < $< > $@ || ( rm -f $@ ; false )
content/docs/changes.md: ../docs/CHANGES.md ../internal/cmds/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmds/generate-chezmoi.io-content-docs -shorttitle="Changes" -longtitle="Changes" < $< > $@ || ( rm -f $@ ; false )

content/docs/comparison.md: ../docs/COMPARISON.md ../internal/cmd/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmd/generate-chezmoi.io-content-docs -shorttitle="Comparison" -longtitle="Comparison Guide" < $< > $@ || ( rm -f $@ ; false )
content/docs/comparison.md: ../docs/COMPARISON.md ../internal/cmds/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmds/generate-chezmoi.io-content-docs -shorttitle="Comparison" -longtitle="Comparison Guide" < $< > $@ || ( rm -f $@ ; false )

content/docs/contributing.md: ../docs/CONTRIBUTING.md ../internal/cmd/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmd/generate-chezmoi.io-content-docs -shorttitle="Contributing" -longtitle="Contributing Guide" < $< > $@ || ( rm -f $@ ; false )
content/docs/contributing.md: ../docs/CONTRIBUTING.md ../internal/cmds/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmds/generate-chezmoi.io-content-docs -shorttitle="Contributing" -longtitle="Contributing Guide" < $< > $@ || ( rm -f $@ ; false )

content/docs/faq.md: ../docs/FAQ.md ../internal/cmd/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmd/generate-chezmoi.io-content-docs -shorttitle="FAQ" -longtitle="Frequently Asked Questions" < $< > $@ || ( rm -f $@ ; false )
content/docs/faq.md: ../docs/FAQ.md ../internal/cmds/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmds/generate-chezmoi.io-content-docs -shorttitle="FAQ" -longtitle="Frequently Asked Questions" < $< > $@ || ( rm -f $@ ; false )

content/docs/how-to.md: ../docs/HOWTO.md ../internal/cmd/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmd/generate-chezmoi.io-content-docs -shorttitle="How-To" -longtitle="How-To Guide" < $< > $@ || ( rm -f $@ ; false )
content/docs/how-to.md: ../docs/HOWTO.md ../internal/cmds/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmds/generate-chezmoi.io-content-docs -shorttitle="How-To" -longtitle="How-To Guide" < $< > $@ || ( rm -f $@ ; false )

content/docs/install.md: ../docs/INSTALL.md ../internal/cmd/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmd/generate-chezmoi.io-content-docs -shorttitle="Install" -longtitle="Install Guide" < $< > $@ || ( rm -f $@ ; false )
content/docs/install.md: ../docs/INSTALL.md ../internal/cmds/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmds/generate-chezmoi.io-content-docs -shorttitle="Install" -longtitle="Install Guide" < $< > $@ || ( rm -f $@ ; false )

content/docs/media.md: ../docs/MEDIA.md ../internal/cmd/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmd/generate-chezmoi.io-content-docs -shorttitle="Media" -longtitle="Media" < $< > $@ || ( rm -f $@ ; false )
content/docs/media.md: ../docs/MEDIA.md ../internal/cmds/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmds/generate-chezmoi.io-content-docs -shorttitle="Media" -longtitle="Media" < $< > $@ || ( rm -f $@ ; false )

content/docs/quick-start.md: ../docs/QUICKSTART.md ../internal/cmd/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmd/generate-chezmoi.io-content-docs -shorttitle="Quick Start" -longtitle="Quick Start Guide" < $< > $@ || ( rm -f $@ ; false )
content/docs/quick-start.md: ../docs/QUICKSTART.md ../internal/cmds/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmds/generate-chezmoi.io-content-docs -shorttitle="Quick Start" -longtitle="Quick Start Guide" < $< > $@ || ( rm -f $@ ; false )

content/docs/reference.md: ../docs/REFERENCE.md ../internal/cmd/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmd/generate-chezmoi.io-content-docs -shorttitle="Reference" -longtitle="Reference Manual" < $< > $@ || ( rm -f $@ ; false )
content/docs/reference.md: ../docs/REFERENCE.md ../internal/cmds/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmds/generate-chezmoi.io-content-docs -shorttitle="Reference" -longtitle="Reference Manual" < $< > $@ || ( rm -f $@ ; false )

content/docs/related.md: ../docs/RELATED.md ../internal/cmd/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmd/generate-chezmoi.io-content-docs -shorttitle="Related" -longtitle="Related Software" < $< > $@ || ( rm -f $@ ; false )
content/docs/related.md: ../docs/RELATED.md ../internal/cmds/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmds/generate-chezmoi.io-content-docs -shorttitle="Related" -longtitle="Related Software" < $< > $@ || ( rm -f $@ ; false )

content/docs/templating.md: ../docs/TEMPLATING.md ../internal/cmd/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmd/generate-chezmoi.io-content-docs -shorttitle="Templating" -longtitle="Templating Guide" < $< > $@ || ( rm -f $@ ; false )
content/docs/templating.md: ../docs/TEMPLATING.md ../internal/cmds/generate-chezmoi.io-content-docs/main.go Makefile
go run ../internal/cmds/generate-chezmoi.io-content-docs -shorttitle="Templating" -longtitle="Templating Guide" < $< > $@ || ( rm -f $@ ; false )
10 changes: 5 additions & 5 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ The important directories in chezmoi are:

| Directory | Contents |
| --------- | -------- |
| `cmd/` | Code for the `chezmoi` command. |
| `docs/` | The documentation single source of truth. Help text, examples, and the [chezmoi.io](https://chezmoi.io) website are generated from the files in this directory, particularly `docs/REFERENCE.md`. |
| `internal/chezmoi/` | chezmoi's core functionality. |
| `testdata/scripts/` | High-level tests of chezmoi's commands using [`testscript`](https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript). |
| `internal/cmd/` | Code for the `chezmoi` command. |
| `internal/cmd/testdata/scripts/` | High-level tests of chezmoi's commands using [`testscript`](https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript). |

## Key concepts

Expand Down Expand Up @@ -111,10 +111,10 @@ persistent state. chezmoi can then detect if a third party has updated a target
since chezmoi last wrote it by comparing the actual state entry in the target
state with the entry state in the persistent state.

## `cmd/`
## `internal/cmd/`

`cmd/*cmd.go` contains the code for each individual command and
`cmd/*templatefuncs.go` contain the template functions.
`internal/cmd/*cmd.go` contains the code for each individual command and
`internal/cmd/*templatefuncs.go` contain the template functions.

Commands are defined as methods on the `Config` struct. The `Config` struct is
large, containing all configuration values read from the config file, command
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 4 additions & 8 deletions main_test.go → internal/cmd/main_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd_test

import (
"bufio"
Expand All @@ -20,8 +20,8 @@ import (
"github.com/twpayne/go-vfs/v3"
"github.com/twpayne/go-vfs/v3/vfst"

"github.com/twpayne/chezmoi/v2/cmd"
"github.com/twpayne/chezmoi/v2/internal/chezmoitest"
"github.com/twpayne/chezmoi/v2/internal/cmd"
)

var umaskConditionRx = regexp.MustCompile(`\Aumask:([0-7]{3})\z`)
Expand Down Expand Up @@ -275,9 +275,9 @@ func cmdMkGPGConfig(ts *testscript.TestScript, neg bool, args []string) {
`encryption = "gpg"`,
`[gpg]`,
` args = [`,
` "--homedir", ` + quote(gpgHomeDir) + `,`,
` "--homedir", ` + strconv.Quote(gpgHomeDir) + `,`,
` "--no-tty",`,
` "--passphrase", ` + quote(passphrase) + `,`,
` "--passphrase", ` + strconv.Quote(passphrase) + `,`,
` "--pinentry-mode", "loopback",`,
` ]`,
}
Expand Down Expand Up @@ -446,10 +446,6 @@ func prependDirToPath(dir, path string) string {
return strings.Join(append([]string{dir}, filepath.SplitList(path)...), string(os.PathListSeparator))
}

func quote(s string) string {
return fmt.Sprintf("%q", s)
}

func setup(env *testscript.Env) error {
var (
binDir = filepath.Join(env.WorkDir, "bin")
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ chezmoi add $HOME${/}.symlink
cmp $CHEZMOISOURCEDIR/symlink_dot_symlink golden/symlink_dot_symlink

# test adding a symlink with a separator
symlink $HOME${/}.symlink2 -> .dir${/}subdir${/}file
symlink $HOME/.symlink2 -> .dir/subdir/file
chezmoi add $HOME${/}.symlink2
cmp $CHEZMOISOURCEDIR/symlink_dot_symlink2 golden/symlink_dot_symlink

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mkhomedir
mksourcedir

# test change file mode
chmod 777 $HOME${/}.file
chmod 777 $HOME/.file
chezmoi apply --force
cmpmod 666 $HOME/.file

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ stdout ^${HOME@R}/\.file\s+${CHEZMOISOURCEDIR@R}/dot_file\s+${WORK@R}/.*/\.file$
# test that chezmoi merge falls back to a two-way merge when the template is invalid
# FIXME the following test fails
# chezmoi merge $HOME${/}.invalid_template
# stdout ^${HOME@R}/\.invalid_template\s+${CHEZMOISOURCEDIR}/dot_invalid_template\.tmpl$
# stdout ^${HOME@R}/\.invalid_template\s+$CHEZMOISOURCEDIR/dot_invalid_template\.tmpl$

-- bin/vimdiff --
#!/bin/sh
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ mksourcedir

# test that chezmoi re-add adds all modified files
chezmoi apply --force
edit $HOME${/}.file
edit $HOME${/}.dir/file
edit $HOME${/}.dir/subdir/file
edit $HOME/.file
edit $HOME/.dir/file
edit $HOME/.dir/subdir/file
chezmoi re-add
grep '# edited' $CHEZMOISOURCEDIR/dot_file
grep '# edited' $CHEZMOISOURCEDIR/dot_dir/file
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ chezmoi apply $HOME${/}.template
cmp $HOME/.template $HOME/.file

# test that chezmoi add --template-symlinks replaces absolute symlinks, pointing to files inside home, with templates
symlink ${HOME}${/}.symlink_absolute -> $HOME/.dir/subdir/file
symlink $HOME/.symlink_absolute -> $HOME/.dir/subdir/file
chezmoi add --template-symlinks $HOME${/}.symlink_absolute
cmp $CHEZMOISOURCEDIR/symlink_dot_symlink_absolute.tmpl golden/symlink_dot_symlink_absolute.tmpl

# test that chezmoi add --template-symlinks replaces absolute symlinks, pointing to files inside the source directory, with templates
symlink $HOME${/}.symlink_source -> $CHEZMOISOURCEDIR/.dir/subdir/file
symlink $HOME/.symlink_source -> $CHEZMOISOURCEDIR/.dir/subdir/file
chezmoi add --template-symlinks $HOME${/}.symlink_source
cmp $CHEZMOISOURCEDIR/symlink_dot_symlink_source.tmpl golden/symlink_dot_symlink_source.tmpl

chhome home2/user

# test that chezmoi add reads add.templateSymlinks from the config file
symlink ${HOME}${/}.symlink_absolute -> $HOME/.dir/subdir/file
symlink $HOME/.symlink_absolute -> $HOME/.dir/subdir/file
chezmoi add --template-symlinks $HOME${/}.symlink_absolute
cmp $CHEZMOISOURCEDIR/symlink_dot_symlink_absolute.tmpl golden/symlink_dot_symlink_absolute.tmpl

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ stdout a${/}b

# test lookPath template function
chezmoi execute-template '{{ lookPath "go" }}'
stdout go${exe}
stdout go$exe

# test stat template function
chezmoi execute-template '{{ (stat ".").isDir }}'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func run() error {
})

// Generate install.sh.
installShTemplate, err := template.ParseFiles("internal/cmd/generate-install.sh/install.sh.tmpl")
installShTemplate, err := template.ParseFiles("internal/cmds/generate-install.sh/install.sh.tmpl")
if err != nil {
return err
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"os"

"github.com/twpayne/chezmoi/v2/cmd"
"github.com/twpayne/chezmoi/v2/internal/cmd"
)

var (
Expand Down

0 comments on commit 4191e4b

Please sign in to comment.