mirror of
https://github.com/spf13/cobra
synced 2025-05-07 22:07:23 +00:00
Omit LICENSE file when licence is set to none
Creating a project without a license should not create an empty LICENSE file.
This commit is contained in:
parent
748b14cfe6
commit
3bba6a2481
3 changed files with 52 additions and 1 deletions
|
@ -5,6 +5,9 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -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, ", "),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -36,9 +36,11 @@ type License struct {
|
||||||
Header string // License header for source files
|
Header string // License header for source files
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var noLicense = License{"None", []string{"none", "false"}, "", ""}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Allows a user to not use a license.
|
// Allows a user to not use a license.
|
||||||
Licenses["none"] = License{"None", []string{"none", "false"}, "", ""}
|
Licenses["none"] = noLicense
|
||||||
|
|
||||||
initApache2()
|
initApache2()
|
||||||
initMit()
|
initMit()
|
||||||
|
|
|
@ -64,6 +64,9 @@ func (p *Project) Create() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// create license
|
// create license
|
||||||
|
if p.Legal.Name == noLicense.Name {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return p.createLicenseFile()
|
return p.createLicenseFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue