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

Omit LICENSE file when license is set to none #937

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 56 additions & 2 deletions cobra/cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
)

func getProject() *Project {
wd, _ := os.Getwd()
return &Project{
AbsolutePath: fmt.Sprintf("%s/testproject", wd),
AbsolutePath: fmt.Sprintf("%s/testproject", mustTempDir()),
Legal: getLicense(),
Copyright: copyrightLine(),
AppName: "testproject",
Expand All @@ -19,6 +22,14 @@ func getProject() *Project {
}
}

func mustTempDir() string {
dir, err := ioutil.TempDir("", "cobra_cli_test_")
if err != nil {
panic(err)
}
return dir
}

func TestGoldenInitCmd(t *testing.T) {
project := getProject()
defer os.RemoveAll(project.AbsolutePath)
Expand All @@ -37,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