From ebf4b4aa1bf5e6c7877b72e10d14154af864ceec Mon Sep 17 00:00:00 2001 From: snopan Date: Mon, 24 Jun 2024 09:12:38 +1000 Subject: [PATCH 1/3] fixes: log write destination before checking dry run --- cmd/mockery.go | 2 +- pkg/outputter.go | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) 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..130f40a7 100644 --- a/pkg/outputter.go +++ b/pkg/outputter.go @@ -354,12 +354,18 @@ func (m *Outputter) Generate(ctx context.Context, iface *Interface) error { } outputPath := pathlib.NewPath(interfaceConfig.Dir).Join(interfaceConfig.FileName) + fileLog := log.With().Stringer(logging.LogKeyFile, outputPath).Logger() + fileLog.Info().Msg("writing to file") + + // If we're in dry-run mode, don't complete make directory and write file operations + 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) From 77d344bd3754fe0f8c18fac9b0a740f431740064 Mon Sep 17 00:00:00 2001 From: snopan Date: Mon, 24 Jun 2024 09:20:40 +1000 Subject: [PATCH 2/3] Change where the comment should be --- pkg/outputter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/outputter.go b/pkg/outputter.go index 130f40a7..e3f916fc 100644 --- a/pkg/outputter.go +++ b/pkg/outputter.go @@ -353,11 +353,11 @@ 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 we're in dry-run mode, don't complete make directory and write file operations if m.dryRun { continue } From 0df9e5c4d53e7686a0782e0f5d15c48f762b8ab1 Mon Sep 17 00:00:00 2001 From: snopan Date: Mon, 24 Jun 2024 14:49:44 +1000 Subject: [PATCH 3/3] Updated Generate tests to check dry run --- pkg/outputter_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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) } }) }