2014-06-04 21:25:38 +00:00
|
|
|
package publish
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/drone/drone/shared/build/buildfile"
|
|
|
|
|
2014-06-12 21:38:12 +00:00
|
|
|
"gopkg.in/yaml.v1"
|
2014-06-04 21:25:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// emulate Build struct
|
|
|
|
type PublishToNPM struct {
|
|
|
|
Publish *Publish `yaml:"publish,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleYml1 = `
|
|
|
|
publish:
|
|
|
|
npm:
|
|
|
|
username: foo
|
|
|
|
email: foo@example.com
|
|
|
|
password: bar
|
|
|
|
`
|
|
|
|
|
|
|
|
var sampleYml2 = `
|
|
|
|
publish:
|
|
|
|
npm:
|
|
|
|
username: foo
|
|
|
|
email: foo@example.com
|
|
|
|
password: bar
|
|
|
|
force: true
|
|
|
|
`
|
|
|
|
|
|
|
|
var sampleYmlWithReg = `
|
|
|
|
publish:
|
|
|
|
npm:
|
|
|
|
username: foo
|
|
|
|
email: foo@example.com
|
|
|
|
password: bar
|
|
|
|
registry: https://npm.example.com/me/
|
|
|
|
folder: my-project/node-app/
|
|
|
|
tag: 1.2.3
|
|
|
|
`
|
|
|
|
|
|
|
|
func setUpWithNPM(input string) (string, error) {
|
|
|
|
var buildStruct PublishToNPM
|
2014-06-12 21:38:12 +00:00
|
|
|
err := yaml.Unmarshal([]byte(input), &buildStruct)
|
2014-06-04 21:25:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
bf := buildfile.New()
|
|
|
|
buildStruct.Publish.Write(bf, nil)
|
|
|
|
return bf.String(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNPMPublish(t *testing.T) {
|
|
|
|
bscr, err := setUpWithNPM(sampleYml1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't unmarshal publish script: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(bscr, "npm publish") {
|
|
|
|
t.Error("Expect script to contain install command")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNPMForcePublish(t *testing.T) {
|
|
|
|
bscr, err := setUpWithNPM(sampleYml2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't unmarshal publish script: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(bscr, "npm publish --force") {
|
|
|
|
t.Error("Expect script to contain install command")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNPMPublishRegistry(t *testing.T) {
|
|
|
|
bscr, err := setUpWithNPM(sampleYmlWithReg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't unmarshal publish script: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(bscr, "npm config set registry https://npm.example.com/me/") {
|
|
|
|
t.Error("Expect script to contain npm config registry command")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(bscr, "npm publish my-project/node-app/ --tag 1.2.3") {
|
|
|
|
t.Error("Expect script to contain npm publish command")
|
|
|
|
}
|
|
|
|
}
|