1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00

refactor: using os.Stat to decide wether it is a file or a dir

This should avoid some failures.
This commit is contained in:
Carlos Alexandro Becker 2017-11-20 22:12:50 -02:00 committed by Carlos Alexandro Becker
parent 03aec11c60
commit dba83bff60
2 changed files with 22 additions and 18 deletions

View File

@ -45,30 +45,34 @@ func (Pipe) Run(ctx *context.Context) error {
}
func checkMain(ctx *context.Context, build config.Build) error {
if strings.HasSuffix(build.Main, ".go") {
file, err := parser.ParseFile(token.NewFileSet(), build.Main, nil, 0)
var main = build.Main
if main == "" {
main = "."
}
stat, ferr := os.Stat(main)
if os.IsNotExist(ferr) {
return errors.Wrapf(ferr, "could not open %s", main)
}
if stat.IsDir() {
packs, err := parser.ParseDir(token.NewFileSet(), main, nil, 0)
if err != nil {
return errors.Wrapf(err, "failed to parse file: %s", build.Main)
return errors.Wrapf(err, "failed to parse dir: %s", main)
}
if hasMain(file) {
return nil
for _, pack := range packs {
for _, file := range pack.Files {
if hasMain(file) {
return nil
}
}
}
return fmt.Errorf("build for %s does not contain a main function", build.Binary)
}
var dir = build.Main
if dir == "" {
dir = "."
}
packs, err := parser.ParseDir(token.NewFileSet(), dir, nil, 0)
file, err := parser.ParseFile(token.NewFileSet(), build.Main, nil, 0)
if err != nil {
return errors.Wrapf(err, "failed to parse dir: %s", dir)
return errors.Wrapf(err, "failed to parse file: %s", build.Main)
}
for _, pack := range packs {
for _, file := range pack.Files {
if hasMain(file) {
return nil
}
}
if hasMain(file) {
return nil
}
return fmt.Errorf("build for %s does not contain a main function", build.Binary)
}

View File

@ -277,7 +277,7 @@ func TestRunPipeWithouMainFunc(t *testing.T) {
})
t.Run("not main.go", func(t *testing.T) {
ctx.Config.Builds[0].Main = "foo.go"
assert.EqualError(t, Pipe{}.Run(ctx), `failed to parse file: foo.go: open foo.go: no such file or directory`)
assert.EqualError(t, Pipe{}.Run(ctx), `could not open foo.go: stat foo.go: no such file or directory`)
})
t.Run("glob", func(t *testing.T) {
ctx.Config.Builds[0].Main = "."