Fixed bug risk and antipattern using deepsource (#904)

## Description

Hi 👋 I ran the [DeepSource](https://deepsource.io/) static analyzer on the forked copy of this repo and found some [interesting code quality issues](https://deepsource.io/gh/ankitdobhal/goreplay/issues/). This PR fixes a few of them.

## Summary of Fixes

- Added .deepsource.toml to fix bug risks
- Empty string test can be improved
- Nested if can be replaced with else-if

## Type of change

- [✔] Antipattern
- [ ] New feature (non-breaking change which adds functionality)

## Checklist:

- [✔] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
This commit is contained in:
Ankit Dobhal
2021-02-23 20:15:24 +03:00
committed by GitHub
parent 5e27c92f80
commit 8f14d5b178
6 changed files with 39 additions and 15 deletions
+28
View File
@@ -0,0 +1,28 @@
version = 1
exclude_patterns = [
"vendor/**"
]
[[analyzers]]
name = "go"
enabled = true
[analyzers.meta]
import_paths = ["github.com/ankitdobhal/goreplay"]
[[analyzers]]
name = "docker"
enabled = true
[[analyzers]]
name = "ruby"
enabled = true
[[analyzers]]
name = "javascript"
enabled = true
[analyzers.meta]
environment = ["nodejs"]
+1 -1
View File
@@ -197,7 +197,7 @@ func (sock *SockRaw) GetSnapLen() int {
func (sock *SockRaw) SetBPFFilter(expr string) error { func (sock *SockRaw) SetBPFFilter(expr string) error {
sock.mu.Lock() sock.mu.Lock()
defer sock.mu.Unlock() defer sock.mu.Unlock()
if len(expr) == 0 { if expr == "" {
return unix.SetsockoptInt(sock.fd, unix.SOL_SOCKET, unix.SO_DETACH_FILTER, 0) return unix.SetsockoptInt(sock.fd, unix.SOL_SOCKET, unix.SO_DETACH_FILTER, 0)
} }
filter, err := pcap.CompileBPFFilter(layers.LinkTypeEthernet, sock.snaplen, expr) filter, err := pcap.CompileBPFFilter(layers.LinkTypeEthernet, sock.snaplen, expr)
+1 -1
View File
@@ -31,7 +31,7 @@ func loggingMiddleware(next http.Handler) http.Handler {
} }
func main() { func main() {
if len(os.Getenv("GOMAXPROCS")) == 0 { if os.Getenv("GOMAXPROCS") == "" {
runtime.GOMAXPROCS(runtime.NumCPU() * 2) runtime.GOMAXPROCS(runtime.NumCPU() * 2)
} }
+2 -3
View File
@@ -162,11 +162,10 @@ func (i *FileInput) init() (err error) {
for _, c := range resp.Contents { for _, c := range resp.Contents {
matches = append(matches, "s3://"+bucket+"/"+(*c.Key)) matches = append(matches, "s3://"+bucket+"/"+(*c.Key))
} }
} else { } else if matches, err = filepath.Glob(i.path); err != nil{
if matches, err = filepath.Glob(i.path); err != nil {
Debug(0, "[INPUT-FILE] Wrong file pattern", i.path, err) Debug(0, "[INPUT-FILE] Wrong file pattern", i.path, err)
return return
}
} }
if len(matches) == 0 { if len(matches) == 0 {
+6 -9
View File
@@ -29,10 +29,9 @@ func TestInputFileWithGET(t *testing.T) {
// The read request should match the original request // The read request should match the original request
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else if !expectedCaptureFile.PayloadsEqual(readPayloads){
if !expectedCaptureFile.PayloadsEqual(readPayloads) {
t.Error("Request read back from file should match") t.Error("Request read back from file should match")
}
} }
} }
@@ -53,10 +52,9 @@ func TestInputFileWithPayloadLargerThan64Kb(t *testing.T) {
// The read request should match the original request // The read request should match the original request
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else if !expectedCaptureFile.PayloadsEqual(readPayloads){
if !expectedCaptureFile.PayloadsEqual(readPayloads) {
t.Error("Request read back from file should match") t.Error("Request read back from file should match")
}
} }
} }
@@ -82,10 +80,9 @@ func TestInputFileWithGETAndPOST(t *testing.T) {
// The read requests should match the original request // The read requests should match the original request
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else if !expectedCaptureFile.PayloadsEqual(readPayloads){
if !expectedCaptureFile.PayloadsEqual(readPayloads) {
t.Error("Request read back from file should match") t.Error("Request read back from file should match")
}
} }
} }
+1 -1
View File
@@ -355,7 +355,7 @@ func HasResponseTitle(payload []byte) bool {
return false return false
} }
// only validate status codes mentioned in rfc2616. // only validate status codes mentioned in rfc2616.
if len(http.StatusText(status)) == 0 { if http.StatusText(status) == "" {
return false return false
} }
// handle cases from #875 // handle cases from #875