mirror of
https://github.com/drone-plugins/drone-npm.git
synced 2026-06-04 18:23:52 +08:00
Move plugin tests
This commit is contained in:
@@ -5,6 +5,13 @@
|
||||
|
||||
package npm
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Settings for the Plugin.
|
||||
type Settings struct {
|
||||
Username string
|
||||
@@ -18,6 +25,9 @@ type Settings struct {
|
||||
Access string
|
||||
}
|
||||
|
||||
// globalRegistry defines the default NPM registry.
|
||||
const globalRegistry = "https://registry.npmjs.org"
|
||||
|
||||
func (p *pluginImpl) Validate() error {
|
||||
// Validate the Config and return an error if there are issues.
|
||||
return nil
|
||||
@@ -27,3 +37,26 @@ func (p *pluginImpl) Exec() error {
|
||||
// Implementation of the plugin.
|
||||
return nil
|
||||
}
|
||||
|
||||
// npmrcContentsUsernamePassword creates the contents from a username and
|
||||
// password
|
||||
func npmrcContentsUsernamePassword(config Settings) string {
|
||||
// get the base64 encoded string
|
||||
authString := fmt.Sprintf("%s:%s", config.Username, config.Password)
|
||||
encoded := base64.StdEncoding.EncodeToString([]byte(authString))
|
||||
|
||||
// create the file contents
|
||||
return fmt.Sprintf("_auth = %s\nemail = %s", encoded, config.Email)
|
||||
}
|
||||
|
||||
/// Writes npmrc contents when using a token
|
||||
func npmrcContentsToken(config Settings) string {
|
||||
registry, _ := url.Parse(config.Registry)
|
||||
registry.Scheme = "" // Reset the scheme to empty. This makes it so we will get a protocol relative URL.
|
||||
registryString := registry.String()
|
||||
|
||||
if !strings.HasSuffix(registryString, "/") {
|
||||
registryString = registryString + "/"
|
||||
}
|
||||
return fmt.Sprintf("%s:_authToken=%s", registryString, config.Token)
|
||||
}
|
||||
|
||||
+38
-2
@@ -7,6 +7,42 @@ package npm
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPlugin(t *testing.T) {
|
||||
t.Skip()
|
||||
func TestTokenRCContents(t *testing.T) {
|
||||
settings := Settings{
|
||||
Registry: "https://npm.someorg.com/",
|
||||
Token: "token",
|
||||
}
|
||||
actual := npmrcContentsToken(settings)
|
||||
expected := "//npm.someorg.com/:_authToken=token"
|
||||
if actual != expected {
|
||||
t.Errorf("Unexpected token settings (Got: %s, Expected: %s)", actual, expected)
|
||||
}
|
||||
|
||||
settings.Registry = "https://npm.someorg.com/with/path/"
|
||||
actual = npmrcContentsToken(settings)
|
||||
expected = "//npm.someorg.com/with/path/:_authToken=token"
|
||||
if actual != expected {
|
||||
t.Errorf("Unexpected token settings (Got: %s, Expected: %s)", actual, expected)
|
||||
}
|
||||
|
||||
settings.Registry = globalRegistry
|
||||
actual = npmrcContentsToken(settings)
|
||||
expected = "//registry.npmjs.org/:_authToken=token"
|
||||
if actual != expected {
|
||||
t.Errorf("Unexpected token settings (Got: %s, Expected: %s)", actual, expected)
|
||||
}
|
||||
|
||||
settings.Registry = "https://npm.someorg.com"
|
||||
actual = npmrcContentsToken(settings)
|
||||
expected = "//npm.someorg.com/:_authToken=token"
|
||||
if actual != expected {
|
||||
t.Errorf("Unexpected token settings (Got: %s, Expected: %s)", actual, expected)
|
||||
}
|
||||
|
||||
settings.Registry = "https://npm.someorg.com/with/path"
|
||||
actual = npmrcContentsToken(settings)
|
||||
expected = "//npm.someorg.com/with/path/:_authToken=token"
|
||||
if actual != expected {
|
||||
t.Errorf("Unexpected token settings (Got: %s, Expected: %s)", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTokenRCContents(t *testing.T) {
|
||||
conf := Config{
|
||||
Registry: "https://npm.someorg.com/",
|
||||
Token: "token",
|
||||
}
|
||||
actual := npmrcContentsToken(conf)
|
||||
expected := "//npm.someorg.com/:_authToken=token"
|
||||
if actual != expected {
|
||||
t.Errorf("Unexpected token config (Got: %s, Expected: %s)", actual, expected)
|
||||
}
|
||||
|
||||
conf.Registry = "https://npm.someorg.com/with/path/"
|
||||
actual = npmrcContentsToken(conf)
|
||||
expected = "//npm.someorg.com/with/path/:_authToken=token"
|
||||
if actual != expected {
|
||||
t.Errorf("Unexpected token config (Got: %s, Expected: %s)", actual, expected)
|
||||
}
|
||||
|
||||
conf.Registry = GlobalRegistry
|
||||
actual = npmrcContentsToken(conf)
|
||||
expected = "//registry.npmjs.org/:_authToken=token"
|
||||
if actual != expected {
|
||||
t.Errorf("Unexpected token config (Got: %s, Expected: %s)", actual, expected)
|
||||
}
|
||||
|
||||
conf.Registry = "https://npm.someorg.com"
|
||||
actual = npmrcContentsToken(conf)
|
||||
expected = "//npm.someorg.com/:_authToken=token"
|
||||
if actual != expected {
|
||||
t.Errorf("Unexpected token config (Got: %s, Expected: %s)", actual, expected)
|
||||
}
|
||||
|
||||
conf.Registry = "https://npm.someorg.com/with/path"
|
||||
actual = npmrcContentsToken(conf)
|
||||
expected = "//npm.someorg.com/with/path/:_authToken=token"
|
||||
if actual != expected {
|
||||
t.Errorf("Unexpected token config (Got: %s, Expected: %s)", actual, expected)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user