Skip to content

Commit

Permalink
Merge pull request #20 from gripmock/add-godocs
Browse files Browse the repository at this point in the history
add godocs
  • Loading branch information
rez1dent3 committed Jul 7, 2024
2 parents a7d747b + 832a0a2 commit f8d061b
Show file tree
Hide file tree
Showing 16 changed files with 760 additions and 100 deletions.
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
- package-ecosystem: gomod
directory: /
schedule:
interval: daily
open-pull-requests-limit: 10
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
14 changes: 14 additions & 0 deletions .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: update changelog

on:
release:
types: [released]

permissions: {}

jobs:
changelog:
permissions:
contents: write
secrets: inherit
uses: bavix/.github/.github/workflows/changelog.yml@0.3.0
26 changes: 26 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: golangci-lint
on:
push:
branches:
- master
pull_request:
permissions:
contents: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.22' ]
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
cache: true
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.59.1
8 changes: 8 additions & 0 deletions .github/workflows/semgrep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: semgrep

on:
pull_request:

jobs:
semgrep:
uses: bavix/.github/.github/workflows/go-semgrep.yml@0.3.0
26 changes: 26 additions & 0 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Unit
on:
pull_request:

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.22' ]
steps:
-
name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
cache: true
- name: Test with Go
run: go test ./...
- name: Upload Go test results
uses: actions/upload-artifact@v4
with:
name: Go-results-${{ matrix.go-version }}
path: TestResults-${{ matrix.go-version }}.json
40 changes: 40 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
run:
timeout: 1m
build-tags:
- wireinject
linters:
enable-all: true
disable:
# turn on later
- godox
# deprecated
- gomnd
- execinquery
# not relevant
- varnamelen
- wrapcheck
- paralleltest
- exhaustruct
linters-settings:
lll:
line-length: 140
gci:
sections:
- Standard
- Default
- Prefix(github.com/gripmock)
depguard:
rules:
main:
allow:
- $gostd
- github.com
issues:
exclude-files:
- pkg/dependencies/builder.go
exclude-dirs:
- example
exclude-rules:
- path: (.+)_test.go
linters:
- dupl
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: *

test:
go test -tags mock -race -cover ./...

lint:
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1 run --color always ${args}

lint-fix:
make lint args=--fix
55 changes: 46 additions & 9 deletions matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,90 @@ import (
"github.com/gripmock/deeply"
)

// match checks if a given query matches a given stub.
//
// It checks if the query matches the stub's input data and headers using
// the equals, contains, and matches methods.
func match(query Query, stub *Stub) bool {
return equals(stub.Input.Equals, query.Data, stub.Input.IgnoreArrayOrder) &&
// Check if the query's input data matches the stub's input data.
dataMatch := equals(stub.Input.Equals, query.Data, stub.Input.IgnoreArrayOrder) &&
contains(stub.Input.Contains, query.Data, stub.Input.IgnoreArrayOrder) &&
matches(stub.Input.Matches, query.Data, stub.Input.IgnoreArrayOrder) &&
equals(stub.Headers.Equals, query.Headers, false) &&
matches(stub.Input.Matches, query.Data, stub.Input.IgnoreArrayOrder)

// Check if the query's headers match the stub's headers.
headersMatch := equals(stub.Headers.Equals, query.Headers, false) &&
contains(stub.Headers.Contains, query.Headers, false) &&
matches(stub.Headers.Matches, query.Headers, false)

// Return true if both the data and headers match, otherwise false.
return dataMatch && headersMatch
}

// rankMatch ranks how well a given query matches a given stub.
//
// It ranks the query's input data and headers against the stub's input data
// and headers using the RankMatch method from the deeply package.
func rankMatch(query Query, stub *Stub) float64 {
rank := deeply.RankMatch(stub.Input.Equals, query.Data) +
// Rank the query's input data against the stub's input data.
dataRank := deeply.RankMatch(stub.Input.Equals, query.Data) +
deeply.RankMatch(stub.Input.Contains, query.Data) +
deeply.RankMatch(stub.Input.Matches, query.Data)

// If the stub has headers, rank the query's headers against the stub's headers.
var headersRank float64
if stub.Headers.Len() > 0 {
rank += deeply.RankMatch(stub.Headers.Equals, query.Headers) +
headersRank = deeply.RankMatch(stub.Headers.Equals, query.Headers) +
deeply.RankMatch(stub.Headers.Contains, query.Headers) +
deeply.RankMatch(stub.Headers.Matches, query.Headers)
}

return rank
// Return the sum of the data and headers ranks.
return dataRank + headersRank
}

// equals checks if the expected map matches the actual value.
//
// It returns true if the expected map matches the actual value,
// otherwise false.
func equals(expected map[string]any, actual any, orderIgnore bool) bool {
if expected == nil || len(expected) == 0 {
// If the expected map is empty or nil, return true.
if len(expected) == 0 {
return true
}

// If orderIgnore is true, use the EqualsIgnoreArrayOrder method from the deeply package.
if orderIgnore {
return deeply.EqualsIgnoreArrayOrder(expected, actual)
}

// Otherwise, use the Equals method from the deeply package.
return deeply.Equals(expected, actual)
}

// contains checks if the expected map is a subset of the actual value.
//
// It returns true if the expected map is a subset of the actual value,
// otherwise false.
func contains(expected map[string]any, actual any, _ bool) bool {
if expected == nil || len(expected) == 0 {
// If the expected map is empty or nil, return true.
if len(expected) == 0 {
return true
}

// Use the ContainsIgnoreArrayOrder method from the deeply package.
return deeply.ContainsIgnoreArrayOrder(expected, actual)
}

// matches checks if the expected map matches the actual value using regular expressions.
//
// It returns true if the expected map matches the actual value using regular expressions,
// otherwise false.
func matches(expected map[string]any, actual any, _ bool) bool {
if expected == nil || len(expected) == 0 {
// If the expected map is empty or nil, return true.
if len(expected) == 0 {
return true
}

// Use the MatchesIgnoreArrayOrder method from the deeply package.
return deeply.MatchesIgnoreArrayOrder(expected, actual)
}
2 changes: 1 addition & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Query struct {
func toggles(r *http.Request) features.Toggles {
var flags []features.Flag

if len(r.Header.Values("X-GripMock-RequestInternal")) > 0 {
if len(r.Header.Values("X-Gripmock-Requestinternal")) > 0 {
flags = append(flags, RequestInternalFlag)
}

Expand Down
2 changes: 1 addition & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestQuery_NewInternal(t *testing.T) {
payload := `{"service":"Testing","method":"TestMethod","data":{"Hola":"Mundo"}}`

req := httptest.NewRequest(http.MethodPost, "/api/stubs/search", bytes.NewReader([]byte(payload)))
req.Header.Add(strings.ToUpper("X-GripMock-RequestInternal"), "ok") //enable
req.Header.Add(strings.ToUpper("X-GripMock-RequestInternal"), "ok")

q, err := stuber.NewQuery(req)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit f8d061b

Please sign in to comment.