diff --git a/cmd/mockery.go b/cmd/mockery.go index 684ffb7b..2f638508 100644 --- a/cmd/mockery.go +++ b/cmd/mockery.go @@ -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 } diff --git a/pkg/outputter.go b/pkg/outputter.go index d35653bc..e3f916fc 100644 --- a/pkg/outputter.go +++ b/pkg/outputter.go @@ -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) diff --git a/pkg/outputter_test.go b/pkg/outputter_test.go index 5e21d821..27387d46 100644 --- a/pkg/outputter_test.go +++ b/pkg/outputter_test.go @@ -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 { @@ -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{}) @@ -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) } }) }