Skip to content

Commit 449496a

Browse files
authored
Fix endpoint printout on startup (#32)
1 parent 1e04d75 commit 449496a

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

offline/handler_instance.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ func (handlerInstance *HandlerInstance) SetExecutionPath(path string) {
3434
}
3535

3636
func (handlerInstance *HandlerInstance) CompileHandler() (inputFilePaths []string) {
37-
dir, err := os.Executable()
37+
workingDirectory, err := os.Getwd()
38+
3839
if err != nil {
3940
println(fmt.Errorf("error fetching executable location: %w", err))
4041
return
4142
}
4243

43-
workingDirectory := filepath.Dir(dir)
44-
4544
result := api.Build(api.BuildOptions{
4645
EntryPoints: []string{handlerInstance.handlerConfig.Source},
4746
Bundle: true,

offline/offline.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func Run(filePath string, moduleName string, port string) error {
2828
panic(fmt.Errorf("error parsing .terrable.toml file: %w", err))
2929
}
3030

31-
printConfig(*terrableConfig)
31+
printConfig(*terrableConfig, port)
3232

3333
var wg sync.WaitGroup
3434
defer wg.Done()
@@ -51,14 +51,14 @@ func Run(filePath string, moduleName string, port string) error {
5151
return nil
5252
}
5353

54-
func printConfig(config config.TerrableConfig) {
54+
func printConfig(config config.TerrableConfig, port string) {
5555
totalEndpoints := 0
5656
printlines := []string{}
5757

5858
for _, handler := range config.Handlers {
5959
for method, path := range handler.Http {
60-
totalEndpoints += len(handler.Http)
61-
printlines = append(printlines, fmt.Sprintf(" %-*s http://localhost:8080%s\n", 5, method, path))
60+
totalEndpoints += 1
61+
printlines = append(printlines, fmt.Sprintf(" %-*s http://localhost:%s%s\n", 5, method, port, path))
6262
}
6363
}
6464

offline/offline_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package offline
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"os"
8+
"strings"
9+
"testing"
10+
11+
"github.com/terrable-dev/terrable/config"
12+
)
13+
14+
func TestPrintConfig(t *testing.T) {
15+
testConfig := config.TerrableConfig{
16+
Handlers: []config.HandlerMapping{
17+
{
18+
Name: "Handler1",
19+
Source: "source1",
20+
Http: map[string]string{
21+
"GET": "/path1",
22+
"POST": "/path1",
23+
},
24+
},
25+
{
26+
Name: "Handler2",
27+
Source: "source2",
28+
Http: map[string]string{
29+
"GET": "/path2",
30+
},
31+
},
32+
},
33+
}
34+
35+
// Capture stdout
36+
old := os.Stdout
37+
r, w, _ := os.Pipe()
38+
os.Stdout = w
39+
40+
// Call the function
41+
printConfig(testConfig, "1234")
42+
43+
// Restore stdout
44+
w.Close()
45+
os.Stdout = old
46+
47+
// Read captured output
48+
var buf bytes.Buffer
49+
io.Copy(&buf, r)
50+
output := buf.String()
51+
52+
expectedEndpoints := 3
53+
expectedLine := fmt.Sprintf("%d Endpoint(s) to prepare...", expectedEndpoints)
54+
if !strings.Contains(output, expectedLine) {
55+
t.Errorf("Expected output to contain '%s', but it doesn't.\nActual output:\n%s", expectedLine, output)
56+
}
57+
58+
expectedLines := []string{
59+
"Starting terrable local server...",
60+
" GET http://localhost:1234/path1",
61+
" POST http://localhost:1234/path1",
62+
" GET http://localhost:1234/path2",
63+
}
64+
65+
for _, line := range expectedLines {
66+
if !strings.Contains(output, line) {
67+
t.Errorf("Expected output to contain '%s', but it doesn't.\nActual output:\n%s", line, output)
68+
}
69+
}
70+
}

terrable_build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = 0.1.4
1+
version = 0.1.5

0 commit comments

Comments
 (0)