Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only generate moq when interface file is newer than output file #179

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/matryer/moq
module github.com/djui/moq
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should not be part of the PR.

(see also all other files, where the import paths are changed)


go 1.18

Expand Down
2 changes: 1 addition & 1 deletion internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"text/template"

"github.com/matryer/moq/internal/registry"
"github.com/djui/moq/internal/registry"
)

// Template is the Moq template. It is capable of generating the Moq
Expand Down
2 changes: 1 addition & 1 deletion internal/template/template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"go/types"
"strings"

"github.com/matryer/moq/internal/registry"
"github.com/djui/moq/internal/registry"
)

// Data is the template data used to render the Moq template.
Expand Down
2 changes: 1 addition & 1 deletion internal/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"go/types"
"testing"

"github.com/matryer/moq/internal/registry"
"github.com/djui/moq/internal/registry"
)

func TestTemplateFuncs(t *testing.T) {
Expand Down
94 changes: 92 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os"
"path/filepath"

"github.com/matryer/moq/pkg/moq"
"github.com/djui/moq/pkg/moq"
)

// Version is the command version, injected at build time.
Expand All @@ -23,6 +23,8 @@ type userFlags struct {
stubImpl bool
skipEnsure bool
remove bool
force bool
verbose bool
args []string
}

Expand All @@ -37,6 +39,8 @@ func main() {
flag.BoolVar(&flags.skipEnsure, "skip-ensure", false,
"suppress mock implementation check, avoid import cycle if mocks generated outside of the tested package")
flag.BoolVar(&flags.remove, "rm", false, "first remove output file, if it exists")
flag.BoolVar(&flags.force, "force", false, "force generation, otherwise check if go generate file is newer than output file")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not backwards compatible. Maybe it is better to preserve the current mode of operation and add a flag to opt-in for the mtime behavior. Maybe the -mtime flag could be used to enable the mtime mode as well as contain the name of the source file as well. If the source file is the empty string, it would fall back to GOFILE env variable.

flag.BoolVar(&flags.verbose, "verbose", false, "verbose mode")

flag.Usage = func() {
fmt.Println(`moq [flags] source-dir interface [interface2 [interface3 [...]]]`)
Expand Down Expand Up @@ -65,6 +69,39 @@ func run(flags userFlags) error {
return errors.New("not enough arguments")
}

inFile := os.Getenv("GOFILE")

if flags.verbose {
if inFile == "" {
fmt.Fprintln(os.Stderr, "Mock in-file is unknown")
} else {
p, err := filepath.Abs(inFile)
if err != nil {
p = flags.outFile
}
fmt.Fprintln(os.Stderr, "Mock in-file is "+p)
}

if flags.outFile == "" {
fmt.Fprintln(os.Stderr, "Mock out-file is stdout")
} else {
p, err := filepath.Abs(flags.outFile)
if err != nil {
p = flags.outFile
}
fmt.Fprintln(os.Stderr, "Mock out-file is "+p)
}
}

if !flags.force {
if !needsRegeneration(inFile, flags.outFile) {
if flags.verbose {
fmt.Fprintln(os.Stderr, "Skipping mock generation as the input file hasn't changed since the mock was generated")
}
return nil
}
}

if flags.remove && flags.outFile != "" {
if err := os.Remove(flags.outFile); err != nil {
if !errors.Is(err, os.ErrNotExist) {
Expand Down Expand Up @@ -96,6 +133,7 @@ func run(flags userFlags) error {
}

if flags.outFile == "" {
fmt.Fprintln(os.Stderr, "Mock written.")
return nil
}

Expand All @@ -105,5 +143,57 @@ func run(flags userFlags) error {
return err
}

return ioutil.WriteFile(flags.outFile, buf.Bytes(), 0600)
err = ioutil.WriteFile(flags.outFile, buf.Bytes(), 0600)

if flags.verbose {
fmt.Fprintln(os.Stderr, "Mock written.")
}

return err
}

func needsRegeneration(inFile, outFile string) bool {
if outFile == "" {
// Assume that the user wants to print to stdout thus we have nothing to
// compare files and timestamps with.
return true
}

if inFile == "" {
// We were not called via go generate, so we have nothing to compare
// with.
return true
}

inInfo, err := os.Stat(inFile)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// Somehow the input file does not exist, which is weird as it's
// only provided by Go generate and there it should always exists,
// but let's assume it's run manually with wrong configuration.
return true
}

// Something went wrong stating the input file, so let's hope
// regeneration should be done and will work.
fmt.Fprintln(os.Stderr, err)
return true
}

outInfo, err := os.Stat(outFile)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// Likely the output file does not exist yet/anymore, so we should
// regenerate.
return true
}

// Something went wrong stating the output file, so let's hope
// regeneration should be done and will work.
fmt.Fprintln(os.Stderr, err)
return true
}

// The actual comparison.
return inInfo.ModTime().After(outInfo.ModTime())
}
4 changes: 2 additions & 2 deletions pkg/moq/moq.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"io"
"strings"

"github.com/matryer/moq/internal/registry"
"github.com/matryer/moq/internal/template"
"github.com/djui/moq/internal/registry"
"github.com/djui/moq/internal/template"
)

// Mocker can generate mock structs.
Expand Down