go-mastodon/cmd/mstdn/main_test.go

136 lines
2.6 KiB
Go
Raw Normal View History

2017-04-14 12:16:18 +02:00
package main
import (
2017-04-14 12:55:52 +02:00
"encoding/json"
2017-04-19 10:05:00 +02:00
"flag"
2017-04-14 12:55:52 +02:00
"io/ioutil"
2017-04-14 12:16:18 +02:00
"os"
"testing"
2017-04-19 10:05:00 +02:00
2022-11-27 05:24:42 +01:00
"github.com/urfave/cli/v2"
2017-04-14 12:16:18 +02:00
)
func TestReadFileFile(t *testing.T) {
b, err := readFile("main.go")
if err != nil {
t.Fatal(err)
}
if len(b) == 0 {
t.Fatalf("should read something: %v", err)
}
}
func TestReadFileStdin(t *testing.T) {
f, err := os.Open("main.go")
if err != nil {
t.Fatal(err)
}
defer f.Close()
stdin := os.Stdin
os.Stdin = f
defer func() {
os.Stdin = stdin
}()
b, err := readFile("-")
if err != nil {
t.Fatal(err)
}
if len(b) == 0 {
t.Fatalf("should read something: %v", err)
}
}
func TestTextContent(t *testing.T) {
tests := []struct {
input string
want string
}{
{input: "", want: ""},
{input: "<p>foo</p>", want: "foo"},
{input: "<p>foo<span>\nbar\n</span>baz</p>", want: "foobarbaz"},
{input: "<p>foo<span>\nbar<br></span>baz</p>", want: "foobar\nbaz"},
}
for _, test := range tests {
got := textContent(test.input)
if got != test.want {
t.Fatalf("want %q but %q", test.want, got)
}
}
}
2017-04-14 12:55:52 +02:00
func TestGetConfig(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "mstdn")
if err != nil {
t.Fatal(err)
}
home := os.Getenv("HOME")
appdata := os.Getenv("APPDATA")
os.Setenv("HOME", tmpdir)
os.Setenv("APPDATA", tmpdir)
defer func() {
os.RemoveAll(tmpdir)
os.Setenv("HOME", home)
os.Setenv("APPDATA", appdata)
}()
2017-04-19 10:05:00 +02:00
app := makeApp()
set := flag.NewFlagSet("test", 0)
set.Parse([]string{"mstdn", "-profile", ""})
c := cli.NewContext(app, set, nil)
file, config, err := getConfig(c)
2017-04-14 12:55:52 +02:00
if err != nil {
t.Fatal(err)
}
if _, err := os.Stat(file); err == nil {
t.Fatal("should not exists")
}
if config.AccessToken != "" {
t.Fatalf("should be empty: %v", config.AccessToken)
}
if config.ClientID == "" {
t.Fatalf("should not be empty")
}
if config.ClientSecret == "" {
t.Fatalf("should not be empty")
}
config.AccessToken = "foo"
b, err := json.MarshalIndent(config, "", " ")
if err != nil {
2017-04-24 12:40:26 +02:00
t.Fatal(err)
2017-04-14 12:55:52 +02:00
}
err = ioutil.WriteFile(file, b, 0700)
if err != nil {
2017-04-24 12:40:26 +02:00
t.Fatal(err)
2017-04-14 12:55:52 +02:00
}
2017-04-19 10:05:00 +02:00
file, config, err = getConfig(c)
2017-04-14 12:55:52 +02:00
if err != nil {
t.Fatal(err)
}
if _, err := os.Stat(file); err != nil {
t.Fatalf("should exists: %v", err)
}
if got := config.AccessToken; got != "foo" {
t.Fatalf("want %q but %q", "foo", got)
}
}
2017-04-14 13:05:18 +02:00
func TestPrompt(t *testing.T) {
readUsername = func() (string, error) {
return "foo", nil
}
readPassword = func() (string, error) {
return "bar", nil
}
username, password, err := prompt()
if err != nil {
t.Fatal(err)
}
if username != "foo" {
t.Fatalf("want %q but %q", "foo", username)
}
if password != "bar" {
t.Fatalf("want %q but %q", "bar", password)
}
}