Skip to content
This repository has been archived by the owner on Nov 6, 2019. It is now read-only.

Commit

Permalink
Add go unit test + fix trailing slash bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Bjerring committed Oct 24, 2017
1 parent dd7c1e9 commit 8768956
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 16 deletions.
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ matrix:
- python -m unittest discover -p "*_test.py"

- language: go
install: go get -u github.com/golang/lint/golint
script: $GOPATH/bin/golint -set_exit_status
install:
- go get -t ./...
- go get -u github.com/golang/lint/golint
script:
- go test -v ./...
- $GOPATH/bin/golint -set_exit_status
12 changes: 4 additions & 8 deletions latest_results_redirect_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"regexp"
"strings"

"appengine"
"appengine/datastore"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
)

// This handler is responsible for redirecting to the Google Cloud Storage API
Expand All @@ -33,12 +33,8 @@ import (
func latestResultsRedirectHandler(w http.ResponseWriter, r *http.Request) {
remainingPath := strings.Replace(r.URL.Path, "/latest/", "", 1)
pathPieces := strings.SplitN(remainingPath, "/", 2)
if len(pathPieces) > 2 {
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}

latestRun, err := getLatestRun(r, pathPieces[0])

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down Expand Up @@ -90,7 +86,7 @@ func getLatestRun(r *http.Request, browser string) (latest TestRun, err error) {

func getResultsURL(latestRun TestRun, testFile *string) (resultsURL string) {
resultsURL = latestRun.ResultsURL
if testFile != nil {
if testFile != nil && *testFile != "" && *testFile != "/" {
// Assumes that result files are under a directory named SHA[0:10].
resultsBase := strings.SplitAfter(resultsURL, "/" + latestRun.Revision)[0];
resultsPieces := strings.Split(resultsURL, "/")
Expand Down
92 changes: 92 additions & 0 deletions latest_results_redirect_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package wptdashboard

import (
"testing"
)

type Case struct {
testRun TestRun
testFile *string
expected string
}

const sha = "abcdef0123"
const resultsURLBase = "https://storage.googleapis.com/wptd/" + sha + "/"
const platform = "chrome-63.0-linux"
const resultsURL = resultsURLBase + "/" + platform + "-summary.json.gz"

func TestGetResultsURL_NoTestFile(t *testing.T) {
checkResult(
t,
Case {
TestRun {
ResultsURL: resultsURL,
Revision: sha,
},
nil,
resultsURL,
})
}

func TestGetResultsURL_EmptyFile(t *testing.T) {
emptyTestFile := ""
checkResult(
t,
Case {
TestRun {
ResultsURL: resultsURL,
Revision: sha,
},
&emptyTestFile,
resultsURL,
})
}

func TestGetResultsURL_TestFile(t *testing.T) {
file := "css/vendor-imports/mozilla/mozilla-central-reftests/flexbox/flexbox-root-node-001b.html"
checkResult(
t,
Case {
TestRun {
ResultsURL: resultsURL,
Revision: sha,
},
&file,
resultsURLBase + platform + "/" + file,
})
}

func TestGetResultsURL_TrailingSlash(t *testing.T) {
trailingSlash := "/"
checkResult(
t,
Case {
TestRun {
ResultsURL: resultsURL,
Revision: sha,
},
&trailingSlash,
resultsURL,
})
}

func checkResult(t *testing.T, c Case) {
got := getResultsURL(c.testRun, c.testFile)
if got != c.expected {
t.Errorf("\nGot:\n%q\nExpected:\n%q", got, c.expected)
}
}
4 changes: 2 additions & 2 deletions populate_dev_data_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"time"
"fmt"

"appengine"
"appengine/datastore"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
)

// Create TestRun entities for local development and testing.
Expand Down
4 changes: 2 additions & 2 deletions test_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"io/ioutil"
"sort"

"appengine"
"appengine/datastore"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
)

// This handler is responsible for all pages that display test results.
Expand Down
4 changes: 2 additions & 2 deletions test_run_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"net/http"
"time"

"appengine"
"appengine/datastore"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
)

func testRunHandler(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 8768956

Please sign in to comment.