Skip to content

Commit

Permalink
Merge pull request #789 from snopan/master
Browse files Browse the repository at this point in the history
Fixes #769 : Mock files getting created in disk when dry-run is enabled
  • Loading branch information
LandonTClipp authored Jun 24, 2024
2 parents ba0fbe9 + 456d20a commit 0650c45
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (r *RootApp) Run() error {
}
ifaceLog.Debug().Msg("config specifies to generate this interface")

outputter := pkg.NewOutputter(&r.Config, boilerplate, true)
outputter := pkg.NewOutputter(&r.Config, boilerplate, r.Config.DryRun)
if err := outputter.Generate(ifaceCtx, iface); err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/outputter.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,19 @@ func (m *Outputter) Generate(ctx context.Context, iface *Interface) error {
return err
}

// Log where the file would be written to before checking whether to create the directories and files
outputPath := pathlib.NewPath(interfaceConfig.Dir).Join(interfaceConfig.FileName)
fileLog := log.With().Stringer(logging.LogKeyFile, outputPath).Logger()
fileLog.Info().Msg("writing to file")

if m.dryRun {
continue
}

if err := outputPath.Parent().MkdirAll(); err != nil {
return stackerr.NewStackErrf(err, "failed to mkdir parents of: %v", outputPath)
}

fileLog := log.With().Stringer(logging.LogKeyFile, outputPath).Logger()
fileLog.Info().Msg("writing to file")
file, err := outputPath.OpenFile(os.O_RDWR | os.O_CREATE | os.O_TRUNC)
if err != nil {
return stackerr.NewStackErrf(err, "failed to open output file for mock: %v", outputPath)
Expand Down
12 changes: 9 additions & 3 deletions pkg/outputter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,17 @@ func TestOutputter_Generate(t *testing.T) {
name string
packagePath string
fields fields
wantErr bool
dryRun bool
}{
{
name: "generate normal",
packagePath: "github.com/vektra/mockery/v2/pkg/fixtures/example_project",
dryRun: false,
},
{
name: "generate normal",
packagePath: "github.com/vektra/mockery/v2/pkg/fixtures/example_project",
dryRun: true,
},
}
for _, tt := range tests {
Expand All @@ -237,7 +243,7 @@ func TestOutputter_Generate(t *testing.T) {
m := &Outputter{
boilerplate: tt.fields.boilerplate,
config: tt.fields.config,
dryRun: true,
dryRun: tt.dryRun,
}
parser := NewParser([]string{})

Expand Down Expand Up @@ -265,7 +271,7 @@ packages:
t.Logf("checking if path exists: %v", mockPath)
exists, err := mockPath.Exists()
require.NoError(t, err)
assert.True(t, exists)
assert.Equal(t, !tt.dryRun, exists)
}
})
}
Expand Down

0 comments on commit 0650c45

Please sign in to comment.