-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encoded-compare false positives fix (#199)
* encoded-compare: require string or []byte type for json-like or yaml-like named vars * encoded-compare: strict match for yml (not yaml) * cosmetic: reorder funcs
- Loading branch information
Showing
6 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
analyzer/testdata/src/encoded-compare-issue196/false_positive_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package encodedcompareissue196 | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRepoFollowSymlink(t *testing.T) { | ||
defer PrepareTestEnv(t)() | ||
session := loginUser(t, "user2") | ||
|
||
assertCase := func(t *testing.T, url, expectedSymlinkURL string, shouldExist bool) { | ||
t.Helper() | ||
|
||
req := NewRequest(t, "GET", url) | ||
resp := session.MakeRequest(t, req, http.StatusOK) | ||
|
||
htmlDoc := NewHTMLParser(t, resp.Body) | ||
symlinkURL, ok := htmlDoc.Find(".file-actions .button[data-kind='follow-symlink']").Attr("href") | ||
if shouldExist { | ||
assert.True(t, ok) | ||
assert.Equal(t, expectedSymlinkURL, symlinkURL) | ||
} else { | ||
assert.False(t, ok) | ||
} | ||
} | ||
|
||
t.Run("Normal", func(t *testing.T) { | ||
defer PrintCurrentTest(t)() | ||
assertCase(t, "/user2/readme-test/src/branch/symlink/README.md?display=source", "/user2/readme-test/src/branch/symlink/some/other/path/awefulcake.txt", true) | ||
}) | ||
} | ||
|
||
func PrepareTestEnv(t *testing.T) func() { return func() {} } | ||
func PrintCurrentTest(t *testing.T) func() { return func() {} } | ||
|
||
func loginUser(t *testing.T, uid string) *session { | ||
return new(session) | ||
} | ||
|
||
type session struct { | ||
} | ||
|
||
func (s *session) MakeRequest(t *testing.T, req *http.Request, expStatusCode int) *Response { | ||
return new(Response) | ||
} | ||
|
||
func NewRequest(t *testing.T, method string, url string) *http.Request { | ||
return new(http.Request) | ||
} | ||
|
||
type Response struct { | ||
Body *bytes.Buffer | ||
} | ||
|
||
// HTMLDoc struct | ||
type HTMLDoc struct { | ||
} | ||
|
||
// NewHTMLParser parse html file | ||
func NewHTMLParser(t testing.TB, body *bytes.Buffer) *HTMLDoc { | ||
t.Helper() | ||
return new(HTMLDoc) | ||
} | ||
|
||
// Find gets the descendants of each element in the current set of | ||
// matched elements, filtered by a selector. It returns a new Selection | ||
// object containing these matched elements. | ||
func (doc *HTMLDoc) Find(selector string) *Selection { | ||
return new(Selection) | ||
} | ||
|
||
type Selection struct { | ||
} | ||
|
||
// Attr gets the specified attribute's value for the first element in the | ||
// Selection. To get the value for each element individually, use a looping | ||
// construct such as Each or Map method. | ||
func (s *Selection) Attr(attrName string) (val string, exists bool) { | ||
return "", false | ||
} |
45 changes: 45 additions & 0 deletions
45
analyzer/testdata/src/encoded-compare-issue198/var_naming_and_type_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package encodedcompareissue198 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCommonResultFromFullResult(t *testing.T) { | ||
jsonData := new(bytes.Buffer) | ||
var cr *commonResult | ||
|
||
crFromJSON := new(commonResult) | ||
err := json.Unmarshal(jsonData.Bytes(), crFromJSON) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, cr, crFromJSON) | ||
} | ||
|
||
func TestCommonResultFromFullAndCompactJSON(t *testing.T) { | ||
compactJSONData := new(bytes.Buffer) | ||
fullJSONData := new(bytes.Buffer) | ||
|
||
crFromCompactJSON := new(commonResult) | ||
crFromFullJSON := new(commonResult) | ||
|
||
err := json.NewDecoder(compactJSONData).Decode(crFromCompactJSON) | ||
require.NoError(t, err) | ||
|
||
err = json.NewDecoder(fullJSONData).Decode(crFromCompactJSON) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, crFromCompactJSON, crFromFullJSON) | ||
|
||
var crFromYML *commonResult | ||
assert.Equal(t, crFromYML, new(commonResult)) | ||
} | ||
|
||
type commonResult struct { | ||
Name string `json:"name"` | ||
Size int64 `json:"size"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters