-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Delni/chore/fix-coverage
Fix coverage
- Loading branch information
Showing
4 changed files
with
159 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
name: Build | ||
name: Tests & Coverage | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
tests: | ||
|
||
runs-on: ubuntu-latest | ||
steps: | ||
|
@@ -14,19 +14,17 @@ jobs: | |
with: | ||
go-version: 1.18 | ||
|
||
- name: Build | ||
run: go build -v | ||
|
||
- name: Test | ||
run: | | ||
go test -v ./... -coverprofile=coverage.out | ||
go test -v ./... -coverpkg=./... -coverprofile=coverage.out | ||
mkdir coverage-report | ||
touch coverage-report/index.html | ||
go tool cover -html=coverage.out -o=coverage-report/index.html | ||
go tool cover -func=coverage.out -o=coverage.out | ||
pct=$(cat coverage.out | grep total | grep -Po '\d+(?:\.\d+)?%') | ||
echo "COVERAGE_PERCENTAGE=$pct" >> $GITHUB_ENV | ||
pct=$(echo $pct | sed "s/%//") | ||
echo "Global coverage: $pct%" | ||
pct_int=${pct/\.*} | ||
if [[ $pct_int -lt 30 ]]; then | ||
color=red | ||
|
@@ -36,8 +34,13 @@ jobs: | |
color=green | ||
fi | ||
echo "COVERAGE_BADGE_COLOR=$(echo $color)" >> $GITHUB_ENV | ||
if [[ $pct_int -lt 70 ]]; then | ||
echo "Coverage is below 70%" | ||
exit 1 | ||
fi | ||
- name: Generate coverage badge | ||
if: github.ref_name == 'main' | ||
uses: emibcn/[email protected] | ||
with: | ||
label: 'Coverage' | ||
|
@@ -46,6 +49,7 @@ jobs: | |
path: 'coverage-report/coverage-badge.svg' | ||
|
||
- name: Publish coverage report to GitHub Pages | ||
if: github.ref_name == 'main' | ||
uses: JamesIves/github-pages-deploy-action@v4 | ||
with: | ||
folder: coverage-report |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package programs | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
func TestSentenceIsFetchingOnRunningFetch(t *testing.T) { | ||
testModel := loadOverHttpModel{ | ||
url: "test_url", | ||
} | ||
|
||
given := testModel.getSentence("P", false) | ||
expect := "P Fetching file from test_url" | ||
|
||
if !strings.Contains(given, expect) { | ||
t.Errorf("got %s, expected %s", given, expect) | ||
} | ||
} | ||
|
||
func TestSentenceIsFetchedOnEndedFetch(t *testing.T) { | ||
testModel := loadOverHttpModel{ | ||
url: "test_url", | ||
} | ||
|
||
given := testModel.getSentence("P", true) | ||
expect := "P Fetched file from test_url" | ||
|
||
if !strings.Contains(given, expect) { | ||
t.Errorf("got %s, expected %s", given, expect) | ||
} | ||
} | ||
|
||
func TestInitiateProgramShouldStoreUrlAndBody(t *testing.T) { | ||
model := initialModel("test_url") | ||
|
||
if model.url != "test_url" { | ||
t.Errorf("got %v, expected %v", "test_url", model.url) | ||
} | ||
|
||
if model.quitting != false { | ||
t.Errorf("got %v, expected %v", false, model.quitting) | ||
} | ||
|
||
if model.body != nil { | ||
t.Errorf("got %v, expected %v", nil, model.body) | ||
} | ||
} | ||
|
||
func TestShouldQuitOnCtrlC(t *testing.T) { | ||
model := initialModel("test_url") | ||
|
||
given, _ := model.Update(tea.KeyMsg{Type: tea.KeyCtrlC}) | ||
quitting := given.(loadOverHttpModel).quitting | ||
if quitting != true { | ||
t.Errorf("got %v, expected %v", quitting, true) | ||
} | ||
} | ||
|
||
func TestShouldQuitOnResponse(t *testing.T) { | ||
model := initialModel("test_url") | ||
|
||
given, _ := model.Update(responseMsg{}) | ||
quitting := given.(loadOverHttpModel).quitting | ||
if quitting != true { | ||
t.Errorf("got %v, expected %v", quitting, true) | ||
} | ||
} | ||
|
||
func TestShouldQuitOnError(t *testing.T) { | ||
model := initialModel("test_url") | ||
|
||
given, _ := model.Update(errMsg{}) | ||
quitting := given.(loadOverHttpModel).quitting | ||
if quitting != true { | ||
t.Errorf("got %v, expected %v", quitting, true) | ||
} | ||
} | ||
|
||
func TestShouldQuitOnQuitting(t *testing.T) { | ||
model := initialModel("test_url") | ||
model.quitting = true | ||
|
||
_, cmd := model.Update(nil) | ||
if cmd == nil { | ||
t.Errorf("got %v, expected %v", cmd, nil) | ||
} | ||
} | ||
|
||
func TestShouldSimplyTickOtherwise(t *testing.T) { | ||
model := initialModel("test_url") | ||
|
||
_, cmd := model.Update(nil) | ||
if cmd != nil { | ||
t.Errorf("got %v, expected tea.Update", cmd) | ||
} | ||
} | ||
|
||
func TestViewShouldPrintSentence(t *testing.T) { | ||
model := initialModel("test_url") | ||
|
||
given := model.View() | ||
expect := "Fetching file from test_url" | ||
|
||
if !strings.Contains(given, expect) { | ||
t.Errorf("got %s, expected %s", given, expect) | ||
} | ||
} | ||
|
||
func TestViewShouldBeEmptyWhenBodyIsLoaded(t *testing.T) { | ||
model := initialModel("test_url") | ||
model.body = []byte{1} | ||
|
||
given := model.View() | ||
expect := "" | ||
|
||
if given != expect { | ||
t.Errorf("got %s, expected %s", given, expect) | ||
} | ||
} |