Skip to content

Commit

Permalink
fix: Fix use of .chezmoiroot in multiple commands
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 29, 2021
1 parent 4572931 commit ccf6971
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
4 changes: 2 additions & 2 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ func (c *Config) newSourceState(
}
}

sourceDirAbsPath, err := c.sourceDirAbsPath()
c.SourceDirAbsPath, err = c.sourceDirAbsPath()
if err != nil {
return nil, err
}
Expand All @@ -1390,7 +1390,7 @@ func (c *Config) newSourceState(
chezmoi.WithMinVersion(minVersion),
chezmoi.WithMode(c.Mode),
chezmoi.WithPriorityTemplateData(c.Data),
chezmoi.WithSourceDir(sourceDirAbsPath),
chezmoi.WithSourceDir(c.SourceDirAbsPath),
chezmoi.WithSystem(c.sourceSystem),
chezmoi.WithTemplateFuncs(c.templateFuncs),
chezmoi.WithTemplateOptions(c.Template.Options),
Expand Down
10 changes: 10 additions & 0 deletions internal/cmd/testdata/scripts/forget.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ chezmoi forget --force $HOME${/}.file
chezmoi state get --bucket=entryState --key=$WORK/home/user/.file
! stdout .

chhome home2/user

# test that chezmoi forget forgets a file when .chezmoiroot is used
chezmoi forget --force $HOME${/}.file
! exists $CHEZMOISOURCEDIR/home/dot_file

-- golden/state-get-dir.json --
{
"type": "dir",
Expand All @@ -34,3 +40,7 @@ chezmoi state get --bucket=entryState --key=$WORK/home/user/.file
"mode": 420,
"contentsSHA256": "634a4dd193c7b3b926d2e08026aa81a416fd41cec52854863b974af422495663"
}
-- home2/user/.local/share/chezmoi/.chezmoiroot --
home
-- home2/user/.local/share/chezmoi/home/dot_file --
# contents of .file
15 changes: 15 additions & 0 deletions internal/cmd/testdata/scripts/merge_unix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ chhome home3/user
chezmoi merge $HOME${/}.file
stdout ^arg\s+${HOME@R}/\.file\s+${CHEZMOISOURCEDIR@R}/dot_file\s+${WORK@R}/.*/\.file$

chhome home4/user

# test that chezmoi merge respects .chezmoiroot
chezmoi merge $HOME${/}.file
stdout ^${HOME@R}/\.file\s+${CHEZMOISOURCEDIR@R}/home/dot_file\s+${WORK@R}/.*/\.file$

-- home/user/.config/chezmoi/chezmoi.toml --
[merge]
command = "echo"
Expand All @@ -44,3 +50,12 @@ stdout ^arg\s+${HOME@R}/\.file\s+${CHEZMOISOURCEDIR@R}/dot_file\s+${WORK@R}/.*/\
args = ["arg"]
-- home3/user/.local/share/chezmoi/dot_file --
# source
-- home4/user/.config/chezmoi/chezmoi.yaml --
merge:
command: "echo"
-- home4/user/.file --
# destination
-- home4/user/.local/share/chezmoi/.chezmoiroot --
home
-- home4/user/.local/share/chezmoi/home/dot_file --
# source
26 changes: 18 additions & 8 deletions internal/cmd/testdata/scripts/sourcepath.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
mksourcedir

# test that chezmoi source-path returns the source directory
chezmoi source-path
cmpenv stdout golden/source-path
stdout ^${CHEZMOISOURCEDIR@R}$

# test that chezmoi source-path target returns the path to a target's source file
chezmoi source-path $HOME${/}.file
cmpenv stdout golden/source-path-file
stdout ^${CHEZMOISOURCEDIR@R}/dot_file$

# test that chezmoi source-path returns an error if the target is not in the source state
! chezmoi source-path $HOME${/}.newfile
stderr 'not in source state'

# test that chezmoi source-path returns an error if the target is not in the destination directory
! chezmoi source-path $WORK${/}etc${/}passwd
stderr 'not in'

-- golden/source-path --
$CHEZMOISOURCEDIR
-- golden/source-path-file --
$CHEZMOISOURCEDIR/dot_file
chhome home2/user

# test that chezmoi source-path target returns the path the target's source file when .chezmoiroot is used
chezmoi source-path $HOME${/}.file
stdout /home/dot_file$

-- home/user/.local/share/chezmoi/dot_file --
# contents of .file
-- home2/user/.local/share/chezmoi/.chezmoiroot --
home
-- home2/user/.local/share/chezmoi/home/dot_file --
# contents of .file

0 comments on commit ccf6971

Please sign in to comment.