Skip to content

Commit

Permalink
omit LICENSE file when licence is set to none (spf13#937)
Browse files Browse the repository at this point in the history
Creating a project without a license should not create an empty LICENSE
file.
  • Loading branch information
rhcarvalho authored and umarcor committed Mar 1, 2020
1 parent 8b2a7dc commit 15ed74b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
46 changes: 46 additions & 0 deletions cobra/cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
)

Expand Down Expand Up @@ -45,3 +48,46 @@ func TestGoldenInitCmd(t *testing.T) {
}
}
}

func TestInitNoLicense(t *testing.T) {
project := getProject()
project.Legal = noLicense
defer os.RemoveAll(project.AbsolutePath)

err := project.Create()
if err != nil {
t.Fatal(err)
}

root := project.AbsolutePath

want := []string{"main.go", "cmd/root.go"}
var got []string
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
relpath, err := filepath.Rel(root, path)
if err != nil {
return err
}
got = append(got, relpath)
return nil
})
if err != nil {
t.Fatalf("walking path %q: %v", root, err)
}
sort.StringSlice(got).Sort()
sort.StringSlice(want).Sort()
if !reflect.DeepEqual(got, want) {
t.Fatalf(
"In %s, found:\n %s\nwant:\n %s",
root,
strings.Join(got, ", "),
strings.Join(want, ", "),
)
}
}
4 changes: 3 additions & 1 deletion cobra/cmd/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ type License struct {
Header string // License header for source files
}

var noLicense = License{"None", []string{"none", "false"}, "", ""}

func init() {
// Allows a user to not use a license.
Licenses["none"] = License{"None", []string{"none", "false"}, "", ""}
Licenses["none"] = noLicense

initApache2()
initMit()
Expand Down
3 changes: 3 additions & 0 deletions cobra/cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func (p *Project) Create() error {
}

// create license
if p.Legal.Name == noLicense.Name {
return nil
}
return p.createLicenseFile()
}

Expand Down

0 comments on commit 15ed74b

Please sign in to comment.