Skip to content

Commit

Permalink
chore: Fix HTTP cache path in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 23, 2021
1 parent cafb7dd commit 3077a1a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
26 changes: 24 additions & 2 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/format/diff"
"github.com/google/gops/agent"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -395,7 +397,6 @@ func newConfig(options ...configOption) (*Config, error) {
// Configuration.
fileSystem: vfs.OSFS,
bds: bds,
httpClient: newCacheHTTPClient(cacheDirAbsPath.Join(httpCacheDirRelPath)),

// Computed configuration.
homeDirAbsPath: homeDirAbsPath,
Expand Down Expand Up @@ -1129,6 +1130,22 @@ func (c *Config) findFirstConfigTemplate() (chezmoi.RelPath, string, []byte, err
return chezmoi.EmptyRelPath, "", nil, nil
}

func (c *Config) getHTTPClient() (*http.Client, error) {
if c.httpClient != nil {
return c.httpClient, nil
}

httpCacheBasePath, err := c.baseSystem.RawPath(c.CacheDirAbsPath.Join(httpCacheDirRelPath))
if err != nil {
return nil, err
}
httpCache := diskcache.New(httpCacheBasePath.String())
httpTransport := httpcache.NewTransport(httpCache)
c.httpClient = httpTransport.Client()

return c.httpClient, nil
}

// gitAutoAdd adds all changes to the git index and returns the new git status.
func (c *Config) gitAutoAdd() (*git.Status, error) {
if err := c.run(c.WorkingTreeAbsPath, c.Git.Command, []string{"add", "."}); err != nil {
Expand Down Expand Up @@ -1324,6 +1341,11 @@ func (c *Config) newRootCmd() (*cobra.Command, error) {
func (c *Config) newSourceState(
ctx context.Context, options ...chezmoi.SourceStateOption,
) (*chezmoi.SourceState, error) {
httpClient, err := c.getHTTPClient()
if err != nil {
return nil, err
}

sourceStateLogger := c.logger.With().Str(logComponentKey, logComponentValueSourceState).Logger()

sourceDirAbsPath := c.SourceDirAbsPath
Expand All @@ -1341,7 +1363,7 @@ func (c *Config) newSourceState(
chezmoi.WithDefaultTemplateDataFunc(c.defaultTemplateData),
chezmoi.WithDestDir(c.DestDirAbsPath),
chezmoi.WithEncryption(c.encryption),
chezmoi.WithHTTPClient(c.httpClient),
chezmoi.WithHTTPClient(httpClient),
chezmoi.WithInterpreters(c.Interpreters),
chezmoi.WithLogger(&sourceStateLogger),
chezmoi.WithMode(c.Mode),
Expand Down
14 changes: 12 additions & 2 deletions internal/cmd/githubtemplatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ func (c *Config) gitHubKeysTemplateFunc(user string) []*github.Key {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

gitHubClient := newGitHubClient(ctx, c.httpClient)
httpClient, err := c.getHTTPClient()
if err != nil {
returnTemplateError(err)
return nil
}
gitHubClient := newGitHubClient(ctx, httpClient)

var allKeys []*github.Key
opts := &github.ListOptions{
Expand Down Expand Up @@ -57,7 +62,12 @@ func (c *Config) gitHubLatestReleaseTemplateFunc(userRepo string) *github.Reposi
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

gitHubClient := newGitHubClient(ctx, c.httpClient)
httpClient, err := c.getHTTPClient()
if err != nil {
returnTemplateError(err)
return nil
}
gitHubClient := newGitHubClient(ctx, httpClient)

release, _, err := gitHubClient.Repositories.GetLatestRelease(ctx, user, repo)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions internal/cmd/upgradecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ func (c *Config) runUpgradeCmd(cmd *cobra.Command, args []string) error {
return errors.New("cannot upgrade dev version to latest released version unless --force is set")
}

client := newGitHubClient(ctx, c.httpClient)
httpClient, err := c.getHTTPClient()
if err != nil {
return err
}
client := newGitHubClient(ctx, httpClient)

// Get the latest release.
rr, _, err := client.Repositories.GetLatestRelease(ctx, c.upgrade.owner, c.upgrade.repo)
Expand Down Expand Up @@ -240,7 +244,11 @@ func (c *Config) downloadURL(ctx context.Context, url string) ([]byte, error) {
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
httpClient, err := c.getHTTPClient()
if err != nil {
return nil, err
}
resp, err := httpClient.Do(req)
if resp != nil {
c.logger.Err(err).
Str("method", req.Method).
Expand Down
10 changes: 0 additions & 10 deletions internal/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ import (
"unicode"

"github.com/google/go-github/v40/github"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
"golang.org/x/oauth2"

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

var (
Expand Down Expand Up @@ -76,12 +72,6 @@ func firstNonEmptyString(ss ...string) string {
return ""
}

// newCacheHTTPClient returns a new http.Client that will cache responses
// according to the HTTP RFC.
func newCacheHTTPClient(cacheDirAbsPath chezmoi.AbsPath) *http.Client {
return httpcache.NewTransport(diskcache.New(cacheDirAbsPath.String())).Client()
}

// newGitHubClient returns a new github.Client configured with an access token
// and a http client, if available.
func newGitHubClient(ctx context.Context, httpClient *http.Client) *github.Client {
Expand Down

0 comments on commit 3077a1a

Please sign in to comment.