Skip to content

Commit

Permalink
Merge pull request #3 from Delni/chore/fix-coverage
Browse files Browse the repository at this point in the history
Fix coverage
  • Loading branch information
Delni authored Jan 5, 2023
2 parents 2cab676 + abcdca5 commit 91a2a7d
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 9 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/ci.yml → .github/workflows/tests.yml
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:
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -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
3 changes: 1 addition & 2 deletions src/preflight/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package preflight

import (
"fmt"
"os"
"os/exec"
"preflight/src/io"
"runtime"
Expand All @@ -18,7 +17,7 @@ func (p PreflightModel) runCheckpoint() tea.Cmd {
interpreter, err := io.GetInterpreterCommand(runtime.GOOS)
if err != nil {
fmt.Println(err)
os.Exit(1)
return tea.Quit
}
interpreterArg := interpreter.InterpreterArgs
if checkpoint.UseInteractive {
Expand Down
27 changes: 26 additions & 1 deletion src/preflight/update_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package preflight

import "testing"
import (
"preflight/src/systemcheck"
"testing"
)

func TestUpdateInternalStateFinished(t *testing.T) {
p := makeTestModel()
Expand All @@ -11,3 +14,25 @@ func TestUpdateInternalStateFinished(t *testing.T) {
t.Errorf("got %+v", ans)
}
}

func TestUpdateInternalStateContinue(t *testing.T) {
p := makeTestModel()
p.checks = []systemcheck.SystemCheck{
testSystemCheck,
testSystemCheck,
}

ans, _ := p.UpdateInternalState(systemCheckMsg{check: true})

if ans.done != false {
t.Errorf("got %+v", ans.done)
}

if ans.activeIndex != 1 {
t.Errorf("got %+v", ans.activeIndex)
}

if ans.progress.Percent() != .5 {
t.Errorf("got %+v", ans.progress.Percent())
}
}
122 changes: 122 additions & 0 deletions src/programs/load_http_file_test.go
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)
}
}

0 comments on commit 91a2a7d

Please sign in to comment.