Move plugin tests

This commit is contained in:
Don
2019-12-18 16:11:56 -08:00
parent ed46e39b10
commit 35966e1092
3 changed files with 71 additions and 47 deletions
+33
View File
@@ -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
View File
@@ -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)
}
}
-45
View File
@@ -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)
}
}