Skip to content

Commit

Permalink
Add tests for license package
Browse files Browse the repository at this point in the history
Signed-off-by: Liam White <[email protected]>
  • Loading branch information
liamawhite committed Jun 30, 2019
1 parent 5aff729 commit 60538ed
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 3 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/liamawhite/licenser

go 1.12

require github.com/spf13/cobra v0.0.5
require (
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.2.2
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
Expand All @@ -13,6 +14,7 @@ github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
Expand All @@ -23,6 +25,7 @@ github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb6
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
Expand Down
3 changes: 1 addition & 2 deletions pkg/license/apache.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ type Apache20 struct {
Year int
Owner string

licenseCache []byte
detectionCache []byte
licenseCache []byte
}

func (a *Apache20) Reader() io.Reader {
Expand Down
58 changes: 58 additions & 0 deletions pkg/license/apache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2019 Liam White
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package license

import (
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestApache20_Reader(t *testing.T) {
t.Run("Reader has correct bytes", func(t *testing.T) {
a := NewApache20(2019, "Test")
want, _ := ioutil.ReadFile("testdata/apache.golden")
got, _ := ioutil.ReadAll(a.Reader())
assert.Equal(t, want, got)
})
}

func TestApache20_IsPresent(t *testing.T) {
tests := []struct {
name string
inputFile string
want bool
}{
{
name: "License is present",
inputFile: "testdata/apache.golden",
want: true,
},
{
name: "License is not present",
inputFile: "testdata/nolicense.golden",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &Apache20{}
inputReader, _ := os.Open(tt.inputFile)
assert.Equal(t, tt.want, a.IsPresent(inputReader))
})
}
}
File renamed without changes.
13 changes: 13 additions & 0 deletions pkg/license/testdata/apache.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2019 Test

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
9 changes: 9 additions & 0 deletions pkg/license/testdata/nolicense.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at vehicula urna, vitae fermentum mauris. Aenean consectetur lectus ligula, vitae finibus lectus pretium eu. Morbi nec mauris purus. Sed ut dictum magna, in luctus turpis. Nam sagittis in eros quis aliquam. Etiam quis semper purus. Cras eget neque quis dui rhoncus tempus. Quisque vehicula eget sapien ac mattis.

Cras suscipit consequat sem, in hendrerit velit sagittis ut. Sed vitae nisl ligula. Morbi molestie sed sem quis condimentum. Fusce interdum rhoncus urna, vitae aliquam orci feugiat ut. Phasellus sollicitudin sagittis aliquam. Maecenas vitae erat nibh. Nam vel turpis dui. Mauris gravida leo aliquam, feugiat nisl in, facilisis erat. Morbi sed nunc elit. In nec felis mauris.

Etiam at eros id nulla interdum rhoncus eget eu orci. Etiam pulvinar vulputate dapibus. Nunc iaculis arcu sit amet lorem posuere maximus nec quis dolor. Donec aliquet diam luctus, luctus justo a, laoreet dolor. Fusce volutpat justo a metus tristique aliquam. Pellentesque maximus libero quam, a posuere justo consequat id. Nunc vel laoreet ipsum. Curabitur malesuada sodales leo vel laoreet. Integer in hendrerit eros, sit amet fermentum justo. Suspendisse in libero iaculis, dictum libero nec, convallis turpis. Aliquam erat volutpat. Phasellus condimentum imperdiet scelerisque.

Donec facilisis lorem at vehicula semper. Vivamus malesuada nulla nulla, et placerat orci dapibus at. Quisque commodo viverra varius. Sed et semper urna. Etiam laoreet est massa, a tempor libero pulvinar in. Vestibulum ac est lectus. Praesent interdum finibus pretium. Donec quis turpis et est tincidunt aliquet. Nunc tempor dolor ac volutpat mollis. Morbi egestas elementum turpis, eget posuere felis commodo sed.

Nunc posuere sollicitudin aliquet. In feugiat vel leo et porta. Pellentesque commodo tempus purus, quis ultrices neque lobortis at. Proin finibus nunc massa, nec vulputate dui molestie ac. In porttitor ac lorem eu viverra. Aliquam eu dignissim nulla. Integer auctor augue non tellus efficitur vestibulum. In sed condimentum orci. Aenean imperdiet enim in malesuada condimentum. Morbi tincidunt ante ligula, nec mattis sapien congue in.

0 comments on commit 60538ed

Please sign in to comment.