Exemple #1
0
func TestShouldSuppressPackages(t *testing.T) {
	data := []struct {
		description string
		packages    []database.Package
		db          databaseMock
		auth        *gddoexp.GithubAuth
		httpClient  httpClientMock
		expected    []gddoexp.SuppressResponse
	}{
		{
			description: "it should suppress all the packages (without authentication)",
			packages: []database.Package{
				{Path: "github.com/rafaeljusto/gddoexp"},
				{Path: "github.com/golang/gddo"},
				{Path: "github.com/miekg/dns"},
				{Path: "github.com/docker/docker"},
				{Path: "github.com/golang/go"},
			},
			db: databaseMock{
				importerCountMock: func(path string) (int, error) {
					return 0, nil
				},
			},
			httpClient: httpClientMock{
				getMock: func(url string) (*http.Response, error) {
					return &http.Response{
						StatusCode: http.StatusOK,
						Body: ioutil.NopCloser(bytes.NewBufferString(`{
  "created_at": "2010-08-03T21:56:23Z",
  "forks_count": 194,
  "network_count": 194,
  "stargazers_count": 1133,
  "updated_at": "` + time.Now().Add(-2*365*24*time.Hour).Format(time.RFC3339) + `"
}`)),
					}, nil
				},
			},
			expected: []gddoexp.SuppressResponse{
				{
					Package:  database.Package{Path: "github.com/docker/docker"},
					Suppress: true,
				},
				{
					Package:  database.Package{Path: "github.com/golang/gddo"},
					Suppress: true,
				},
				{
					Package:  database.Package{Path: "github.com/golang/go"},
					Suppress: true,
				},
				{
					Package:  database.Package{Path: "github.com/miekg/dns"},
					Suppress: true,
				},
				{
					Package:  database.Package{Path: "github.com/rafaeljusto/gddoexp"},
					Suppress: true,
				},
			},
		},
		{
			description: "it should suppress all the packages (authenticated)",
			packages: []database.Package{
				{Path: "github.com/rafaeljusto/gddoexp"},
				{Path: "github.com/golang/gddo"},
				{Path: "github.com/miekg/dns"},
				{Path: "github.com/docker/docker"},
				{Path: "github.com/golang/go"},
			},
			db: databaseMock{
				importerCountMock: func(path string) (int, error) {
					return 0, nil
				},
			},
			auth: &gddoexp.GithubAuth{
				ID:     "exampleuser",
				Secret: "abc123",
			},
			httpClient: httpClientMock{
				getMock: func(url string) (*http.Response, error) {
					if !strings.HasSuffix(url, "?client_id=exampleuser&client_secret=abc123") {
						return &http.Response{
							StatusCode: http.StatusBadRequest,
						}, nil
					}

					return &http.Response{
						StatusCode: http.StatusOK,
						Body: ioutil.NopCloser(bytes.NewBufferString(`{
  "created_at": "2010-08-03T21:56:23Z",
  "forks_count": 194,
  "network_count": 194,
  "stargazers_count": 1133,
  "updated_at": "` + time.Now().Add(-2*365*24*time.Hour).Format(time.RFC3339) + `"
}`)),
					}, nil
				},
			},
			expected: []gddoexp.SuppressResponse{
				{
					Package:  database.Package{Path: "github.com/docker/docker"},
					Suppress: true,
				},
				{
					Package:  database.Package{Path: "github.com/golang/gddo"},
					Suppress: true,
				},
				{
					Package:  database.Package{Path: "github.com/golang/go"},
					Suppress: true,
				},
				{
					Package:  database.Package{Path: "github.com/miekg/dns"},
					Suppress: true,
				},
				{
					Package:  database.Package{Path: "github.com/rafaeljusto/gddoexp"},
					Suppress: true,
				},
			},
		},
		{
			description: "it should suppress all the packages (cache hits)",
			packages: []database.Package{
				{Path: "github.com/rafaeljusto/gddoexp"},
				{Path: "github.com/golang/gddo"},
				{Path: "github.com/miekg/dns"},
				{Path: "github.com/docker/docker"},
				{Path: "github.com/golang/go"},
			},
			db: databaseMock{
				importerCountMock: func(path string) (int, error) {
					return 0, nil
				},
			},
			httpClient: httpClientMock{
				getMock: func(url string) (*http.Response, error) {
					return &http.Response{
						StatusCode: http.StatusOK,
						Header: http.Header{
							"Cache": []string{"1"},
						},
						Body: ioutil.NopCloser(bytes.NewBufferString(`{
  "created_at": "2010-08-03T21:56:23Z",
  "forks_count": 194,
  "network_count": 194,
  "stargazers_count": 1133,
  "updated_at": "` + time.Now().Add(-2*365*24*time.Hour).Format(time.RFC3339) + `"
}`)),
					}, nil
				},
			},
			expected: []gddoexp.SuppressResponse{
				{
					Package:  database.Package{Path: "github.com/docker/docker"},
					Suppress: true,
					Cache:    true,
				},
				{
					Package:  database.Package{Path: "github.com/golang/gddo"},
					Suppress: true,
					Cache:    true,
				},
				{
					Package:  database.Package{Path: "github.com/golang/go"},
					Suppress: true,
					Cache:    true,
				},
				{
					Package:  database.Package{Path: "github.com/miekg/dns"},
					Suppress: true,
					Cache:    true,
				},
				{
					Package:  database.Package{Path: "github.com/rafaeljusto/gddoexp"},
					Suppress: true,
					Cache:    true,
				},
			},
		},
	}

	httpClientBkp := gddoexp.HTTPClient
	defer func() {
		gddoexp.HTTPClient = httpClientBkp
	}()

	isCacheResponseBkp := gddoexp.IsCacheResponse
	defer func() {
		gddoexp.IsCacheResponse = isCacheResponseBkp
	}()
	gddoexp.IsCacheResponse = func(r *http.Response) bool {
		return r.Header.Get("Cache") == "1"
	}

	for i, item := range data {
		gddoexp.HTTPClient = item.httpClient

		var responses []gddoexp.SuppressResponse
		for response := range gddoexp.ShouldSuppressPackages(item.packages, item.db, item.auth) {
			responses = append(responses, response)
		}

		sort.Sort(bySuppressResponsePath(responses))
		if !reflect.DeepEqual(item.expected, responses) {
			t.Errorf("[%d] %s: mismatch responses.\n%v", i, item.description, diff(item.expected, responses))
		}
	}
}
Exemple #2
0
func main() {
	clientID := flag.String("id", "", "Github client ID")
	clientSecret := flag.String("secret", "", "Github client secret")
	output := flag.String("output", "gddoexp.out", "Output file")
	progress := flag.Bool("progress", false, "Show a progress bar")
	flag.Parse()

	var auth *gddoexp.GithubAuth
	if (clientID != nil && *clientID != "") || (clientSecret != nil && *clientSecret != "") {
		if *clientID == "" || *clientSecret == "" {
			fmt.Println("to enable Gthub authentication, you need to inform the id and secret")
			flag.PrintDefaults()
			return
		}

		auth = &gddoexp.GithubAuth{
			ID:     *clientID,
			Secret: *clientSecret,
		}
	}

	// add cache to avoid repeated requests to Github
	gddoexp.HTTPClient = &http.Client{
		Transport: httpcache.NewTransport(
			diskcache.New(path.Join(os.Getenv("HOME"), ".gddoexp")),
		),
	}

	db, err := database.New()
	if err != nil {
		fmt.Println("error connecting to database:", err)
		return
	}

	pkgs, err := db.AllPackages()
	if err != nil {
		fmt.Println("error retrieving all packages:", err)
		return
	}

	file, err := os.OpenFile(*output, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		fmt.Println("error creating output file:", err)
		return
	}
	defer file.Close()

	log.SetOutput(file)
	log.Println("BEGIN")
	log.Printf("%d packages will be analyzed", len(pkgs))

	var progressBar *pb.ProgressBar
	if progress != nil && *progress {
		progressBar = pb.StartNew(len(pkgs))
	}

	var cache int

	for response := range gddoexp.ShouldSuppressPackages(pkgs, db, auth) {
		if progress != nil && *progress {
			progressBar.Increment()
		}

		if response.Cache {
			cache++
		}

		if response.Error != nil {
			log.Println(response.Error)
		} else if response.Suppress {
			log.Printf("package “%s” should be suppressed\n", response.Package.Path)
			if progress != nil && !*progress {
				fmt.Println(response.Package.Path)
			}
		}
	}

	if progress != nil && *progress {
		progressBar.Finish()
	}

	log.Println("Cache hits:", cache)
	log.Println("END")
}