github.com/kvattikuti/drone@v0.2.1-0.20140603034306-d400229a327a/pkg/plugin/publish/npm_test.go (about) 1 package publish 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/drone/drone/pkg/build/buildfile" 8 9 "launchpad.net/goyaml" 10 ) 11 12 // emulate Build struct 13 type PublishToNPM struct { 14 Publish *Publish `yaml:"publish,omitempty"` 15 } 16 17 var sampleYml1 = ` 18 publish: 19 npm: 20 username: foo 21 email: foo@example.com 22 password: bar 23 ` 24 25 var sampleYml2 = ` 26 publish: 27 npm: 28 username: foo 29 email: foo@example.com 30 password: bar 31 force: true 32 ` 33 34 var sampleYmlWithReg = ` 35 publish: 36 npm: 37 username: foo 38 email: foo@example.com 39 password: bar 40 registry: https://npm.example.com/me/ 41 folder: my-project/node-app/ 42 tag: 1.2.3 43 ` 44 45 func setUpWithNPM(input string) (string, error) { 46 var buildStruct PublishToNPM 47 err := goyaml.Unmarshal([]byte(input), &buildStruct) 48 if err != nil { 49 return "", err 50 } 51 bf := buildfile.New() 52 buildStruct.Publish.Write(bf, nil) 53 return bf.String(), err 54 } 55 56 func TestNPMPublish(t *testing.T) { 57 bscr, err := setUpWithNPM(sampleYml1) 58 if err != nil { 59 t.Fatalf("Can't unmarshal publish script: %s", err) 60 } 61 62 if !strings.Contains(bscr, "npm publish") { 63 t.Error("Expect script to contain install command") 64 } 65 } 66 67 func TestNPMForcePublish(t *testing.T) { 68 bscr, err := setUpWithNPM(sampleYml2) 69 if err != nil { 70 t.Fatalf("Can't unmarshal publish script: %s", err) 71 } 72 73 if !strings.Contains(bscr, "npm publish --force") { 74 t.Error("Expect script to contain install command") 75 } 76 } 77 78 func TestNPMPublishRegistry(t *testing.T) { 79 bscr, err := setUpWithNPM(sampleYmlWithReg) 80 if err != nil { 81 t.Fatalf("Can't unmarshal publish script: %s", err) 82 } 83 84 if !strings.Contains(bscr, "npm config set registry https://npm.example.com/me/") { 85 t.Error("Expect script to contain npm config registry command") 86 } 87 88 if !strings.Contains(bscr, "npm publish my-project/node-app/ --tag 1.2.3") { 89 t.Error("Expect script to contain npm publish command") 90 } 91 }