Skip to content

Commit

Permalink
Merge pull request #1 from newmo-oss/add-files
Browse files Browse the repository at this point in the history
Add source files
  • Loading branch information
tenntenn authored Dec 3, 2024
2 parents ecb3031 + 4a031d8 commit 1a8421e
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changelog:
exclude:
labels:
- tagpr
16 changes: 16 additions & 0 deletions .github/workflows/tagpr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# .github/workflows/tagpr.yml
name: tagpr
on:
push:
branches: ["main"]
jobs:
deploy:
runs-on: ubuntu-24.04
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/[email protected]
- uses: Songmu/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35 changes: 35 additions & 0 deletions .tagpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# config file for the tagpr in git config format
# The tagpr generates the initial configuration, which you can rewrite to suit your environment.
# CONFIGURATIONS:
# tagpr.releaseBranch
# Generally, it is "main." It is the branch for releases. The pcpr tracks this branch,
# creates or updates a pull request as a release candidate, or tags when they are merged.
#
# tagpr.versionFile
# Versioning file containing the semantic version needed to be updated at release.
# It will be synchronized with the "git tag".
# Often this is a meta-information file such as gemspec, setup.cfg, package.json, etc.
# Sometimes the source code file, such as version.go or Bar.pm, is used.
# If you do not want to use versioning files but only git tags, specify the "-" string here.
# You can specify multiple version files by comma separated strings.
#
# tagpr.vPrefix
# Flag whether or not v-prefix is added to semver when git tagging. (e.g. v1.2.3 if true)
# This is only a tagging convention, not how it is described in the version file.
#
# tagpr.changelog (Optional)
# Flag whether or not changelog is added or changed during the release.
#
# tagpr.command (Optional)
# Command to change files just before release.
#
# tagpr.tmplate (Optional)
# Pull request template in go template format
#
# tagpr.release (Optional)
# GitHub Release creation behavior after tagging [true, draft, false]
# If this value is not set, the release is to be created.
[tagpr]
vPrefix = true
releaseBranch = main
versionFile = version.txt
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 newmo.tech
Copyright (c) 2024 newmo, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
# gotestingmock
gotestingmock mocking utilities for unit test in Go
# gotestingmock [![Go Reference](https://pkg.go.dev/badge/github.com/newmo-oss/gotestingmocko.svg)](https://pkg.go.dev/github.com/newmo-oss/gotestingmock)[![Go Report Card](https://goreportcard.com/badge/github.com/newmo-oss/gotestingmock)](https://goreportcard.com/report/github.com/newmo-oss/gotestingmock)

gotestingmock mocking utilities for unit test in Go.

## Usage

```go
func Test(t *testing.T) {
t.Parallel()

// gotestingmock.Run simulates a test function on a goroutine.
got := gotestingmock.Run(func(tb *gotestingmock.TB) {
// The test helper can use *gotestingmock.TB as testing.TB
// which is implemented by testing.T, testing.B and testing.F.
MyTestHelper(tb, "arg1")
})

// Check if the test helper failed with t.Error, t.Fatal or similar methods.
if !got.Failed {
t.Error("expected failed did not occur")
}

// Check that the test helper has panicked.
if got.PanicValue != nil {
t.Error("unexpected panic:", got.PanicValue)
}
}
```

## License
MIT
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/newmo-oss/gotestingmock

go 1.22.9
202 changes: 202 additions & 0 deletions testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package gotestingmock

import (
"runtime"
"sync"
"testing"
)

// TB is mock for testing.TB.
// XxxFunc is a mock function for the method Xxx of testing.TB.
// If you confirm more usage please see [Run].
type TB struct {
record Record

// mock funcs
CleanupFunc func(func())
ErrorFunc func(args ...any)
ErrorfFunc func(format string, args ...any)
FailFunc func()
FailNowFunc func()
FailedFunc func() bool
FatalFunc func(args ...any)
FatalfFunc func(format string, args ...any)
HelperFunc func()
LogFunc func(args ...any)
LogfFunc func(format string, args ...any)
NameFunc func() string
SetenvFunc func(key, value string)
SkipFunc func(args ...any)
SkipNowFunc func()
SkipfFunc func(format string, args ...any)
SkippedFunc func() bool
TempDirFunc func() string

testing.TB // for private method and unsupport method
}

// Record records the result of [Run].
type Record struct {
Failed bool
Skipped bool
Goexit bool
PanicValue any
}

// Run runs the given mocking test function with [testing.TB].
// The f can be described in the same way as the test function of the go test.
// Run call the f on new goroutine.
// The return value records whether the test function failed (e.g. t.Error),
// was skipped (e.g. t.Skip), failed and exited its goroutine (e.g. t.Fatal)
// or panic occured.
func Run(f func(*TB)) *Record {
var (
tb TB
wg sync.WaitGroup
ret *Record
)
wg.Add(1)
go func() {
defer func() {
if p := recover(); p != nil {
tb.record.PanicValue = p
}
record := tb.record // copy
ret = &record
wg.Done()
}()
f(&tb)
}()
wg.Wait()
return ret
}

func (tb *TB) Cleanup(f func()) {
if tb.CleanupFunc != nil {
tb.CleanupFunc(f)
}
}

func (tb *TB) Error(args ...any) {
tb.record.Failed = true
if tb.ErrorFunc != nil {
tb.ErrorFunc(args...)
}
}

func (tb *TB) Errorf(format string, args ...any) {
tb.record.Failed = true
if tb.ErrorfFunc != nil {
tb.ErrorfFunc(format, args...)
}
}

func (tb *TB) Fail() {
tb.record.Failed = true
if tb.FailFunc != nil {
tb.FailFunc()
}
}

func (tb *TB) FailNow() {
tb.record.Failed = true
tb.record.Goexit = true
if tb.FailNowFunc != nil {
tb.FailNowFunc()
} else {
runtime.Goexit()
}
}

func (tb *TB) Failed() bool {
if tb.FailedFunc != nil {
return tb.FailedFunc()
}
return tb.record.Failed
}

func (tb *TB) Fatal(args ...any) {
tb.record.Failed = true
tb.record.Goexit = true
if tb.FatalFunc != nil {
tb.FatalFunc(args...)
}
runtime.Goexit()
}

func (tb *TB) Fatalf(format string, args ...any) {
tb.record.Failed = true
tb.record.Goexit = true
if tb.FatalfFunc != nil {
tb.FatalfFunc(format, args...)
}
runtime.Goexit()
}

func (tb *TB) Helper() {
if tb.HelperFunc != nil {
tb.HelperFunc()
}
}

func (tb *TB) Log(args ...any) {
if tb.LogFunc != nil {
tb.LogFunc(args...)
}
}

func (tb *TB) Logf(format string, args ...any) {
if tb.LogfFunc != nil {
tb.LogfFunc(format, args...)
}
}

func (tb *TB) Name() string {
if tb.NameFunc != nil {
return tb.NameFunc()
}
return ""
}

func (tb *TB) Setenv(key, value string) {
if tb.SetenvFunc != nil {
tb.SetenvFunc(key, value)
}
}

func (tb *TB) Skip(args ...any) {
tb.record.Skipped = true
if tb.SkipFunc != nil {
tb.SkipFunc(args...)
}
}

func (tb *TB) SkipNow() {
tb.record.Skipped = true
tb.record.Goexit = true
if tb.SkipNowFunc != nil {
tb.SkipNowFunc()
}
runtime.Goexit()
}

func (tb *TB) Skipf(format string, args ...any) {
tb.record.Skipped = true
if tb.SkipfFunc != nil {
tb.SkipfFunc(format, args...)
}
}

func (tb *TB) Skipped() bool {
if tb.SkippedFunc != nil {
return tb.SkippedFunc()
}
return tb.record.Skipped
}

func (tb *TB) TempDir() string {
if tb.TempDirFunc != nil {
return tb.TempDirFunc()
}
return ""
}
Loading

0 comments on commit 1a8421e

Please sign in to comment.