2024-05-06 18:29:57 +00:00
|
|
|
package plugin
|
2024-05-05 20:14:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2024-05-06 19:04:48 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-05-05 20:14:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWriteNetrc(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
path string
|
|
|
|
machine string
|
|
|
|
login string
|
|
|
|
password string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid input",
|
|
|
|
path: t.TempDir(),
|
|
|
|
machine: "example.com",
|
|
|
|
login: "user",
|
|
|
|
password: "pass",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
err := WriteNetrc(tt.path, tt.machine, tt.login, tt.password)
|
|
|
|
if tt.wantErr {
|
2024-05-06 19:04:48 +00:00
|
|
|
assert.Error(t, err)
|
2024-05-05 20:14:55 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-06 19:04:48 +00:00
|
|
|
assert.NoError(t, err)
|
2024-05-05 20:14:55 +00:00
|
|
|
|
|
|
|
netrcPath := filepath.Join(tt.path, ".netrc")
|
|
|
|
_, err = os.Stat(netrcPath)
|
2024-05-06 19:04:48 +00:00
|
|
|
assert.NoError(t, err)
|
2024-05-05 20:14:55 +00:00
|
|
|
|
|
|
|
content, err := os.ReadFile(netrcPath)
|
2024-05-06 19:04:48 +00:00
|
|
|
assert.NoError(t, err)
|
2024-05-05 20:14:55 +00:00
|
|
|
|
|
|
|
expected := fmt.Sprintf("machine %s\nlogin %s\npassword %s\n", tt.machine, tt.login, tt.password)
|
2024-05-06 19:04:48 +00:00
|
|
|
assert.Equal(t, expected, string(content))
|
2024-05-05 20:14:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|