Beispiel #1
0
func TestGenerateUniqueFile(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
	if err != nil {
		log.Fatal(err)
	}
	client := api.NewClient(nil, inf)

	testutil.ShouldExistFile(t, 0)

	app := command.New(inf, client, os.Stdout, os.Stderr)
	app.Run([]string{"qiitactl", "generate", "file", "Example Title"})
	app.Run([]string{"qiitactl", "generate", "file", "Example Title"})

	testutil.ShouldExistFile(t, 2)

	path := fmt.Sprintf("mine/%s/Example Title.md", time.Now().Format("2006/01/02"))
	_, err = os.Stat(path)
	if err != nil {
		t.Errorf("file should exist at %s", path)
	}
	path = fmt.Sprintf("mine/%s/Example Title-.md", time.Now().Format("2006/01/02"))
	_, err = os.Stat(path)
	if err != nil {
		t.Errorf("file should exist at %s", path)
	}
}
Beispiel #2
0
func TestFetchPostWithID(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	mux := http.NewServeMux()
	handleItem(mux)
	serverMine := httptest.NewServer(mux)
	defer serverMine.Close()
	err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
	if err != nil {
		log.Fatal(err)
	}
	client := api.NewClient(func(subDomain, path string) (url string) {
		switch subDomain {
		case "":
			url = fmt.Sprintf("%s%s%s", serverMine.URL, "/api/v2", path)
		default:
			log.Fatalf("wrong sub domain \"%s\"", subDomain)
		}
		return
	}, inf)

	testutil.ShouldExistFile(t, 0)

	errBuf := bytes.NewBuffer([]byte{})
	app := command.New(inf, client, os.Stdout, errBuf)
	app.Run([]string{"qiitactl", "fetch", "post", "-i", "4bd431809afb1bb99e4f"})
	e := errBuf.Bytes()
	if len(e) != 0 {
		t.Fatal(string(e))
	}

	testutil.ShouldExistFile(t, 1)

	b, err := ioutil.ReadFile("mine/2000/01/01/Example Title.md")
	if err != nil {
		t.Fatal(err)
	}
	actual := string(b)
	expected := `<!--
id: 4bd431809afb1bb99e4f
url: https://qiita.com/yaotti/items/4bd431809afb1bb99e4f
created_at: 2000-01-01T09:00:00+09:00
updated_at: 2000-01-01T09:00:00+09:00
private: false
coediting: false
tags:
- Ruby:
  - 0.0.1
team: null
-->

# Example Title

## Example body`
	if actual != expected {
		t.Errorf("wrong body:\n%s", testutil.Diff(expected, actual))
	}
}
Beispiel #3
0
func main() {
	godotenv.Load()
	g := MustAsset(".goxc.json")
	info, err := info.New(g)
	if err != nil {
		panic("fail to load bindata")
	}
	client := api.NewClient(nil, info)
	cmd := command.New(info, client, os.Stdout, os.Stderr)
	cmd.Run(os.Args)
}
Beispiel #4
0
func TestShowPosts(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	mux := http.NewServeMux()
	handleAuthenticatedUserItems(mux)
	handleTeams(mux)
	serverMine := httptest.NewServer(mux)
	defer serverMine.Close()
	mux = http.NewServeMux()
	handleAuthenticatedUserItemsWithTeam(mux)
	serverTeam := httptest.NewServer(mux)
	defer serverTeam.Close()
	err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
	if err != nil {
		log.Fatal(err)
	}
	client := api.NewClient(func(subDomain, path string) (url string) {
		switch subDomain {
		case "":
			url = fmt.Sprintf("%s%s%s", serverMine.URL, "/api/v2", path)
		case "increments":
			url = fmt.Sprintf("%s%s%s", serverTeam.URL, "/api/v2", path)
		default:
			log.Fatalf("wrong sub domain \"%s\"", subDomain)
		}
		return
	}, inf)

	testutil.ShouldExistFile(t, 0)

	buf := bytes.NewBuffer([]byte{})
	errBuf := bytes.NewBuffer([]byte{})
	app := command.New(inf, client, buf, errBuf)
	app.Run([]string{"qiitactl", "show", "posts"})
	e := errBuf.Bytes()
	if len(e) != 0 {
		t.Fatal(string(e))
	}

	testutil.ShouldExistFile(t, 0)

	if string(buf.Bytes()) != `Posts in Qiita:
4bd431809afb1bb99e4f 2000/01/01 Example Title
Posts in Qiita:Team (Increments Inc.):
4bd431809afb1bb99e4t 2015/09/25 Example Title in team
` {
		t.Errorf("written text is wrong: %s", buf.Bytes())
	}
}
Beispiel #5
0
func TestFetchPostWithWrongID(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	mux := http.NewServeMux()
	handleItem(mux)
	serverMine := httptest.NewServer(mux)
	defer serverMine.Close()
	err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
	if err != nil {
		log.Fatal(err)
	}
	client := api.NewClient(func(subDomain, path string) (url string) {
		switch subDomain {
		case "":
			url = fmt.Sprintf("%s%s%s", serverMine.URL, "/api/v2", path)
		default:
			log.Fatalf("wrong sub domain \"%s\"", subDomain)
		}
		return
	}, inf)

	testutil.ShouldExistFile(t, 0)

	buf := bytes.NewBuffer([]byte{})
	errBuf := bytes.NewBuffer([]byte{})
	app := command.New(inf, client, buf, errBuf)
	app.Run([]string{"qiitactl", "fetch", "post", "-i", "XXXXXXXXXXXXXXXXXXXX"})
	e := errBuf.Bytes()
	actual := string(e)
	expected := "404 Not Found\n"
	if actual != expected {
		t.Fatalf("error should occur when fetches post with wrong ID: %s", actual)
	}

	testutil.ShouldExistFile(t, 0)
}
Beispiel #6
0
func TestNew(t *testing.T) {
	client := api.NewClient(func(subDomain, path string) (url string) {
		return
	}, inf)
	command.New(inf, client, os.Stdout, os.Stderr)
}
Beispiel #7
0
func TestUpdatePostWithNoServer(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	mux := http.NewServeMux()
	serverMine := httptest.NewServer(mux)
	defer serverMine.Close()
	err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
	if err != nil {
		log.Fatal(err)
	}
	client := api.NewClient(func(subDomain, path string) (url string) {
		switch subDomain {
		case "":
			url = fmt.Sprintf("%s%s%s", serverMine.URL, "/api/v2", path)
		default:
			log.Fatalf("wrong sub domain \"%s\"", subDomain)
		}
		return
	}, inf)

	testutil.ShouldExistFile(t, 0)

	err = os.MkdirAll("mine/2000/01/01", 0755)
	if err != nil {
		t.Fatal(err)
	}
	err = ioutil.WriteFile("mine/2000/01/01/Example Title.md", []byte(`<!--
id: 4bd431809afb1bb99e4f
url: https://qiita.com/yaotti/items/4bd431809afb1bb99e4f
created_at: 2000-01-01T09:00:00+09:00
updated_at: 2000-01-01T09:00:00+09:00
private: false
coediting: false
tags:
- Ruby:
  - 0.0.1
- NewTag:
  - "1.0"
team: null
-->

# Example Edited Title

## Example Edited Body`), 0664)
	if err != nil {
		t.Fatal(err)
	}

	testutil.ShouldExistFile(t, 1)

	errBuf := bytes.NewBuffer([]byte{})
	app := command.New(inf, client, os.Stdout, errBuf)
	app.Run([]string{"qiitactl", "update", "post", "mine/2000/01/01/Example Title.md"})
	e := errBuf.Bytes()
	if len(e) == 0 {
		t.Fatal("error should occur")
	}

	testutil.ShouldExistFile(t, 1)
}
Beispiel #8
0
func TestFetchPostsErrors(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	func() {
		mux := http.NewServeMux()
		handleTeams(mux)
		serverMine := httptest.NewServer(mux)
		defer serverMine.Close()
		mux = http.NewServeMux()
		handleAuthenticatedUserItemsWithTeam(mux)
		serverTeam := httptest.NewServer(mux)
		defer serverTeam.Close()
		err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
		if err != nil {
			log.Fatal(err)
		}
		client := api.NewClient(func(subDomain, path string) (url string) {
			switch subDomain {
			case "":
				url = fmt.Sprintf("%s%s%s", serverMine.URL, "/api/v2", path)
			case "increments":
				url = fmt.Sprintf("%s%s%s", serverTeam.URL, "/api/v2", path)
			default:
				log.Fatalf("wrong sub domain \"%s\"", subDomain)
			}
			return
		}, inf)

		buf := bytes.NewBuffer([]byte{})
		errBuf := bytes.NewBuffer([]byte{})
		app := command.New(inf, client, buf, errBuf)
		app.Run([]string{"qiitactl", "fetch", "posts"})
		e := errBuf.Bytes()
		if len(e) == 0 {
			t.Fatal("error should occur")
		}
	}()

	func() {
		mux := http.NewServeMux()
		handleAuthenticatedUserItems(mux)
		serverMine := httptest.NewServer(mux)
		defer serverMine.Close()
		mux = http.NewServeMux()
		handleAuthenticatedUserItemsWithTeam(mux)
		serverTeam := httptest.NewServer(mux)
		defer serverTeam.Close()
		err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
		if err != nil {
			log.Fatal(err)
		}
		client := api.NewClient(func(subDomain, path string) (url string) {
			switch subDomain {
			case "":
				url = fmt.Sprintf("%s%s%s", serverMine.URL, "/api/v2", path)
			case "increments":
				url = fmt.Sprintf("%s%s%s", serverTeam.URL, "/api/v2", path)
			default:
				log.Fatalf("wrong sub domain \"%s\"", subDomain)
			}
			return
		}, inf)

		buf := bytes.NewBuffer([]byte{})
		errBuf := bytes.NewBuffer([]byte{})
		app := command.New(inf, client, buf, errBuf)
		app.Run([]string{"qiitactl", "fetch", "posts"})
		e := errBuf.Bytes()
		if len(e) == 0 {
			t.Fatal("error should occur")
		}
	}()

	func() {
		mux := http.NewServeMux()
		handleAuthenticatedUserItems(mux)
		handleTeams(mux)
		serverMine := httptest.NewServer(mux)
		defer serverMine.Close()
		mux = http.NewServeMux()
		serverTeam := httptest.NewServer(mux)
		defer serverTeam.Close()
		err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
		if err != nil {
			log.Fatal(err)
		}
		client := api.NewClient(func(subDomain, path string) (url string) {
			switch subDomain {
			case "":
				url = fmt.Sprintf("%s%s%s", serverMine.URL, "/api/v2", path)
			case "increments":
				url = fmt.Sprintf("%s%s%s", serverTeam.URL, "/api/v2", path)
			default:
				log.Fatalf("wrong sub domain \"%s\"", subDomain)
			}
			return
		}, inf)

		buf := bytes.NewBuffer([]byte{})
		errBuf := bytes.NewBuffer([]byte{})
		app := command.New(inf, client, buf, errBuf)
		app.Run([]string{"qiitactl", "fetch", "posts"})
		e := errBuf.Bytes()
		if len(e) == 0 {
			t.Fatal("error should occur")
		}
	}()
}