Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move handler_test.go to internal/server package #284

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions cmd/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ const (
internalServerPath = "internal/server"
internalDatabasePath = "internal/database"
gitHubActionPath = ".github/workflows"
testHandlerPath = "tests"
)

// ExitCLI checks if the Project has been exited, and closes
Expand Down Expand Up @@ -326,19 +325,6 @@ func (p *Project) CreateMainFile() error {
return err
}

err = p.CreatePath(testHandlerPath, projectPath)
if err != nil {
log.Printf("Error creating path: %s", projectPath)
cobra.CheckErr(err)
return err
}
// inject testhandler template
err = p.CreateFileWithInjection(testHandlerPath, projectPath, "handler_test.go", "tests")
if err != nil {
cobra.CheckErr(err)
return err
}

makeFile, err := os.Create(filepath.Join(projectPath, "Makefile"))
if err != nil {
cobra.CheckErr(err)
Expand Down Expand Up @@ -560,6 +546,13 @@ func (p *Project) CreateMainFile() error {
cobra.CheckErr(err)
return err
}

err = p.CreateFileWithInjection(internalServerPath, projectPath, "routes_test.go", "tests")
if err != nil {
cobra.CheckErr(err)
return err
}

err = p.CreateFileWithInjection(internalServerPath, projectPath, "server.go", "server")
if err != nil {
log.Printf("Error injecting server.go file: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/template/framework/files/makefile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ docker-down:
# Test the application
test:
@echo "Testing..."
@go test ./tests -v
@go test ./... -v

# Clean the binary
clean:
Expand Down
5 changes: 2 additions & 3 deletions cmd/template/framework/files/tests/default-test.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package tests
package server
import (
"io"
"net/http"
"net/http/httptest"
"{{.ProjectName}}/internal/server"
"testing"
)
func TestHandler(t *testing.T) {
s := &server.Server{}
s := &Server{}
server := httptest.NewServer(http.HandlerFunc(s.HelloWorldHandler))
defer server.Close()
resp, err := http.Get(server.URL)
Expand Down
5 changes: 2 additions & 3 deletions cmd/template/framework/files/tests/echo-test.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package tests
package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"{{.ProjectName}}/internal/server"
"testing"
"github.com/labstack/echo/v4"
)
Expand All @@ -13,7 +12,7 @@ func TestHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
resp := httptest.NewRecorder()
c := e.NewContext(req, resp)
s := &server.Server{}
s := &Server{}
// Assertions
if err := s.HelloWorldHandler(c); err != nil {
t.Errorf("handler() error = %v", err)
Expand Down
5 changes: 2 additions & 3 deletions cmd/template/framework/files/tests/fiber-test.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package tests
package server
import (
"io"
"net/http"
"{{.ProjectName}}/internal/server"
"testing"
"github.com/gofiber/fiber/v2"
)
func TestHandler(t *testing.T) {
// Create a Fiber app for testing
app := fiber.New()
// Inject the Fiber app into the server
s := &server.FiberServer{App: app}
s := &FiberServer{App: app}
// Define a route in the Fiber app
app.Get("/", s.HelloWorldHandler)
// Create a test HTTP request
Expand Down
5 changes: 2 additions & 3 deletions cmd/template/framework/files/tests/gin-test.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package tests
package server
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"{{.ProjectName}}/internal/server"
)
func TestHelloWorldHandler(t *testing.T) {
s := &server.Server{}
s := &Server{}
r := gin.New()
r.GET("/", s.HelloWorldHandler)
// Create a test HTTP request
Expand Down
3 changes: 1 addition & 2 deletions docs/docs/advanced-flag/tailwind.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ The project tree would look like this:
├── internal/
│ └── server/
│ ├── routes.go
│ ├── routes_test.go
│ └── server.go
├── tests/
│ └── handler_test.go
├── go.mod
├── go.sum
├── Makefile
Expand Down
3 changes: 1 addition & 2 deletions docs/docs/blueprint-core/db-drivers.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ Integrating a database adds a new layer to the project structure, primarily in t
│ │ └── database.go
│ └── /server
│ ├── routes.go
│ ├── routes_test.go
│ └── server.go
├── /tests
│ └── handler_test.go
├── go.mod
├── go.sum
├── Makefile
Expand Down
5 changes: 2 additions & 3 deletions docs/docs/blueprint-core/frameworks.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ The project is structured with a simple layout, focusing on the cmd, internal, a
├── /internal
│ └── /server
│ ├── routes.go
│ ├── routes_test.go
│ └── server.go
├── /tests
│ └── handler_test.go
├── go.mod
├── go.sum
├── Makefile
└── README.md
```
```
4 changes: 2 additions & 2 deletions docs/docs/creating-project/makefile.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ make docker-down
```

- **Test the Application:**
Executes tests defined in the `./tests` directory.
Executes all tests defined in the project.
```bash
make test
```
Expand All @@ -46,4 +46,4 @@ Monitors file changes and automatically rebuilds and restarts the application us
make watch
```

Makefile simplifies common development tasks, making it easier to build, run, test, and manage dependencies in a Go project. It enhances productivity by providing a standardized approach to project management.
Makefile simplifies common development tasks, making it easier to build, run, test, and manage dependencies in a Go project. It enhances productivity by providing a standardized approach to project management.
3 changes: 1 addition & 2 deletions docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ Here's an overview of the project structure created by Go Blueprint when all opt
│ │ └── database.go # File containing functions related to database operations.
│ └── server/
│ ├── routes.go # File defining HTTP routes.
│ ├── routes_test.go # Test file for testing HTTP handlers.
│ └── server.go # Main server logic.
├── tests/
│ └── handler_test.go # Test file for testing HTTP handlers.
├── .air.toml # Configuration file for Air, a live-reload utility.
├── docker-compose.yml # Docker Compose configuration for defining DB config.
├── .env # Environment configuration file.
Expand Down