github.com/sona-tar/ghs@v0.0.0-20170415134710-bed1b2953748/ghs_test.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"reflect"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  func Test_Ghs(t *testing.T) {
    17  	assert := func(result interface{}, want interface{}) {
    18  		if !reflect.DeepEqual(result, want) {
    19  			t.Errorf("Returned %+v, want %+v", result, want)
    20  		}
    21  	}
    22  
    23  	Setup()
    24  	defer Teardown()
    25  
    26  	// Normal response
    27  	handler := func(w http.ResponseWriter, r *http.Request) {
    28  		w.Header().Add("Link", HeaderLink(100, 10))
    29  		var items []string
    30  		for i := 1; i < 100+1; i++ {
    31  			items = append(items, fmt.Sprintf(`{"id":%d, "full_name": "test/search_word%d", "description":"%d"}`, i, i, i))
    32  		}
    33  		fmt.Fprintf(w, `{"total_count": 1000, "items": [%s]}`, strings.Join(items, ","))
    34  	}
    35  	mux.HandleFunc("/search/repositories", handler)
    36  
    37  	num, err := ghs(strings.Split(fmt.Sprintf("-e %s -m 1000 SEARCH_WORD", server.URL), " "))
    38  	assert(num, 1000)
    39  	assert(err, nil)
    40  
    41  	num, err = ghs(strings.Split(fmt.Sprintf("-e %s -m 110 SEARCH_WORD", server.URL), " "))
    42  	assert(num, 110)
    43  	assert(err, nil)
    44  
    45  	num, err = ghs(strings.Split("-v", " "))
    46  	assert(num, 0)
    47  	assert(err, nil)
    48  
    49  	num, err = ghs(strings.Split("-h", " "))
    50  	assert(num, 0)
    51  	assert(err, errors.New("help or parse error"))
    52  
    53  	num, err = ghs(strings.Split("-wrong_option", " "))
    54  	assert(num, 0)
    55  	assert(err, errors.New("help or parse error"))
    56  
    57  	num, err = ghs(strings.Split("-s stars", " "))
    58  	assert(num, 0)
    59  	assert(err, errors.New("Parse option error."))
    60  }
    61  
    62  func Test_GhsTokenTest(t *testing.T) {
    63  	assert := func(result interface{}, want interface{}) {
    64  		if !reflect.DeepEqual(result, want) {
    65  			t.Errorf("Returned %+v, want %+v", result, want)
    66  		}
    67  	}
    68  
    69  	Setup()
    70  	defer Teardown()
    71  
    72  	// Normal response
    73  	handler := func(w http.ResponseWriter, r *http.Request) {
    74  		w.Header().Add("Link", HeaderLink(100, 10))
    75  		var items []string
    76  		for i := 1; i < 100+1; i++ {
    77  			items = append(items, fmt.Sprintf(`{"id":%d, "full_name": "test/search_word%d", "description":"%d"}`, i, i, i))
    78  		}
    79  		fmt.Fprintf(w, `{"total_count": 1000, "items": [%s]}`, strings.Join(items, ","))
    80  	}
    81  	mux.HandleFunc("/search/repositories", handler)
    82  
    83  	num, err := ghs(strings.Split(fmt.Sprintf("-t abcdefg -e %s -m 100 SEARCH_WORD", server.URL), " "))
    84  	assert(num, 100)
    85  	assert(err, nil)
    86  
    87  	os.Setenv("GITHUB_TOKEN", "abcdefg")
    88  	num, err = ghs(strings.Split(fmt.Sprintf("-e %s -m 100 SEARCH_WORD", server.URL), " "))
    89  	assert(num, 100)
    90  	assert(err, nil)
    91  	os.Setenv("GITHUB_TOKEN", "")
    92  
    93  	panicIf := func(err error) {
    94  		if err != nil {
    95  			panic(err)
    96  		}
    97  	}
    98  	must := panicIf
    99  
   100  	run := func(cmd string, args ...string) error {
   101  		return exec.Command(cmd, args...).Run()
   102  	}
   103  
   104  	tmpHome, err := ioutil.TempDir("", "go-gitconfig")
   105  	panicIf(err)
   106  
   107  	repoDir := filepath.Join(tmpHome, "repo")
   108  	must(os.Setenv("HOME", tmpHome))
   109  	must(os.MkdirAll(repoDir, 0777))
   110  	must(os.Chdir(repoDir))
   111  
   112  	must(run("git", "init"))
   113  	must(run("git", "config", "--global", "github.token", "abcdefg"))
   114  
   115  	num, err = ghs(strings.Split(fmt.Sprintf("-e %s -m 100 SEARCH_WORD", server.URL), " "))
   116  	assert(num, 100)
   117  	assert(err, nil)
   118  	must(os.RemoveAll(tmpHome))
   119  }
   120  func Test_GhsInvalidResponse(t *testing.T) {
   121  	assert := func(result interface{}, want interface{}) {
   122  		if !reflect.DeepEqual(result, want) {
   123  			t.Errorf("Returned %+v, want %+v", result, want)
   124  		}
   125  	}
   126  
   127  	Setup()
   128  	defer Teardown()
   129  
   130  	// Invalid response
   131  	handler := func(w http.ResponseWriter, r *http.Request) {
   132  		w.Header().Set("Content-Type", "text/plain")
   133  		w.WriteHeader(http.StatusNotFound)
   134  	}
   135  	mux.HandleFunc("/search/repositories", handler)
   136  
   137  	num, err := ghs(strings.Split(fmt.Sprintf("-e %s -m 100 SEARCH_WORD", server.URL), " "))
   138  	assert(num, 0)
   139  	assert(strings.Contains(err.Error(), "404"), true)
   140  }
   141  
   142  func Example_CheckVersion() {
   143  	os.Setenv("GHS_PRINT", "yes")
   144  	defer os.Setenv("GHS_PRINT", "no")
   145  
   146  	CheckVersion("0.0.1")
   147  	// Output:
   148  	// 0.0.1 is not latest, you should upgrade to 0.0.10
   149  	// -> $ brew update && brew upgrade sona-tar/tools/ghs
   150  }
   151  
   152  func Example_PrintHelp() {
   153  	os.Setenv("GHS_PRINT", "yes")
   154  	defer os.Setenv("GHS_PRINT", "no")
   155  	args := strings.Split("ghs -h", " ")[1:]
   156  	flags, _ := NewFlags(args)
   157  	flags.PrintHelp()
   158  	// Output:
   159  	// Usage:
   160  	//   ghs [OPTION] "QUERY"
   161  	//
   162  	// Application Options:
   163  	//   -f, --fields=     limits what fields are searched. 'name', 'description', or
   164  	//                     'readme'.
   165  	//   -k, --fork=       Forked repositories icluded in results. 'true', 'only' or
   166  	//                     'false'.
   167  	//   -s, --sort=       The sort field. 'stars', 'forks', or 'updated'. (default:
   168  	//                     best match)
   169  	//   -o, --order=      The sort order. 'asc' or 'desc'. (default: desc)
   170  	//   -l, --language=   searches repositories based on the language they’re
   171  	//                     written in.
   172  	//   -u, --user=       limits searches to a specific user name.
   173  	//   -r, --repo=       limits searches to a specific repository.
   174  	//   -m, --max=        limits number of result. range 1-1000 (default: 100)
   175  	//   -v, --version     print version infomation and exit.
   176  	//   -e, --enterprise= search from github enterprise.
   177  	//   -t, --token=      Github API token to avoid Github API rate
   178  	//   -h, --help=       Show this help message
   179  	//
   180  	// Github search APIv3 QUERY infomation:
   181  	//   https://developer.github.com/v3/search/
   182  	//   https://help.github.com/articles/searching-repositories/
   183  	//
   184  	// Version:
   185  	//   ghs 0.0.10 (https://github.com/sona-tar/ghs.git)
   186  }
   187  
   188  func Example_PrintfDebug() {
   189  	os.Setenv("GHS_PRINT", "yes")
   190  	Printf("test")
   191  	// Output:
   192  	// test
   193  	os.Setenv("GHS_PRINT", "no")
   194  
   195  	os.Setenv("GHS_DEBUG", "yes")
   196  
   197  	Debug("test")
   198  	// Output:
   199  	// test
   200  	os.Setenv("GHS_DEBUG", "")
   201  }