Skip to content

Commit

Permalink
Merge pull request #606 from LandonTClipp/truncate
Browse files Browse the repository at this point in the history
Set O_TRUNC on mock output files
  • Loading branch information
LandonTClipp committed Apr 20, 2023
2 parents 5cf2282 + 6899a71 commit ad74577
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/outputter.go
Expand Up @@ -288,7 +288,7 @@ func (m *Outputter) Generate(ctx context.Context, iface *Interface) error {

fileLog := log.With().Stringer(logging.LogKeyFile, outputPath).Logger()
fileLog.Info().Msg("writing to file")
file, err := outputPath.OpenFile(os.O_RDWR | os.O_CREATE)
file, err := outputPath.OpenFile(os.O_RDWR | os.O_CREATE | os.O_TRUNC)
if err != nil {
return errors.Wrapf(err, "failed to open output file for mock: %v", outputPath)
}
Expand Down
67 changes: 67 additions & 0 deletions pkg/outputter_test.go
Expand Up @@ -3,12 +3,16 @@ package pkg
import (
"context"
"errors"
"fmt"
"reflect"
"testing"

"github.com/chigopher/pathlib"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
pkgMocks "github.com/vektra/mockery/v2/mocks/github.com/vektra/mockery/v2/pkg"
"github.com/vektra/mockery/v2/pkg/config"
"github.com/vektra/mockery/v2/pkg/logging"
)

func TestFilenameBare(t *testing.T) {
Expand Down Expand Up @@ -144,3 +148,66 @@ func Test_parseConfigTemplates(t *testing.T) {
})
}
}

func TestOutputter_Generate(t *testing.T) {
type fields struct {
boilerplate string
config *config.Config
}

tests := []struct {
name string
packagePath string
fields fields
wantErr bool
}{
{
name: "generate normal",
packagePath: "github.com/vektra/mockery/v2/pkg/fixtures/example_project",
},
}
for _, tt := range tests {
if tt.fields.config == nil {
tt.fields.config = &config.Config{}
}
tt.fields.config.Dir = t.TempDir()
tt.fields.config.MockName = "Mock{{.InterfaceName}}"
tt.fields.config.FileName = "mock_{{.InterfaceName}}.go"

t.Run(tt.name, func(t *testing.T) {
m := &Outputter{
boilerplate: tt.fields.boilerplate,
config: tt.fields.config,
dryRun: true,
}
parser := NewParser([]string{})

log, err := logging.GetLogger("INFO")
require.NoError(t, err)
ctx := log.WithContext(context.Background())

confPath := pathlib.NewPath(t.TempDir()).Join("config.yaml")
ymlContents := fmt.Sprintf(`
packages:
%s:
config:
all: True
`, tt.packagePath)
require.NoError(t, confPath.WriteFile([]byte(ymlContents)))
m.config.Config = confPath.String()

require.NoError(t, parser.ParsePackages(ctx, []string{tt.packagePath}))
require.NoError(t, parser.Load())
for _, intf := range parser.Interfaces() {
t.Logf("generating interface: %s %s", intf.QualifiedName, intf.Name)
require.NoError(t, m.Generate(ctx, intf))
mockPath := pathlib.NewPath(tt.fields.config.Dir).Join("mock_" + intf.Name + ".go")

t.Logf("checking if path exists: %v", mockPath)
exists, err := mockPath.Exists()
require.NoError(t, err)
assert.True(t, exists)
}
})
}
}

0 comments on commit ad74577

Please sign in to comment.