Skip to content

Commit 5c1fe58

Browse files
committed
fix Caddy module tests
1 parent 45b5bfe commit 5c1fe58

6 files changed

Lines changed: 34 additions & 6 deletions

File tree

.github/workflows/windows.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,6 @@ jobs:
180180
$env:PHPRC = Get-Location
181181
182182
go test ./...
183-
#cd caddy
184-
#go test ./...
183+
cd caddy
184+
go test ./...
185185
working-directory: ${{ github.workspace }}\frankenphp

caddy/caddy_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ import (
2121

2222
var testPort = "9080"
2323

24+
// skipIfSymlinkNotValid skips the test if the given path is not a valid symlink
25+
func skipIfSymlinkNotValid(t *testing.T, path string) {
26+
t.Helper()
27+
28+
info, err := os.Lstat(path)
29+
if err != nil {
30+
t.Skipf("symlink test skipped: cannot stat %s: %v", path, err)
31+
}
32+
33+
if info.Mode()&os.ModeSymlink == 0 {
34+
t.Skipf("symlink test skipped: %s is not a symlink (git may not support symlinks on this platform)", path)
35+
}
36+
}
37+
38+
// escapeMetricLabel escapes backslashes in label values for Prometheus text format
39+
func escapeMetricLabel(s string) string {
40+
return strings.ReplaceAll(s, "\\", "\\\\")
41+
}
42+
2443
func TestPHP(t *testing.T) {
2544
var wg sync.WaitGroup
2645
tester := caddytest.NewTester(t)
@@ -548,6 +567,7 @@ func TestWorkerMetrics(t *testing.T) {
548567
`, "caddyfile")
549568

550569
workerName, _ := fastabs.FastAbs("../testdata/index.php")
570+
workerName = escapeMetricLabel(workerName)
551571

552572
// Make some requests
553573
for i := range 10 {
@@ -731,6 +751,7 @@ func TestAutoWorkerConfig(t *testing.T) {
731751
`, "caddyfile")
732752

733753
workerName, _ := fastabs.FastAbs("../testdata/index.php")
754+
workerName = escapeMetricLabel(workerName)
734755

735756
// Make some requests
736757
for i := range 10 {
@@ -804,6 +825,7 @@ func TestAllDefinedServerVars(t *testing.T) {
804825
expectedBody = strings.ReplaceAll(expectedBody, "{documentRoot}", documentRoot)
805826
expectedBody = strings.ReplaceAll(expectedBody, "\r\n", "\n")
806827
expectedBody = strings.ReplaceAll(expectedBody, "{testPort}", testPort)
828+
expectedBody = strings.ReplaceAll(expectedBody, documentRoot+"/", documentRoot+string(filepath.Separator))
807829
tester := caddytest.NewTester(t)
808830
tester.InitServer(`
809831
{
@@ -1505,6 +1527,7 @@ func TestLog(t *testing.T) {
15051527
func TestSymlinkWorkerPaths(t *testing.T) {
15061528
cwd, _ := os.Getwd()
15071529
publicDir := filepath.Join(cwd, "..", "testdata", "symlinks", "public")
1530+
skipIfSymlinkNotValid(t, publicDir)
15081531

15091532
t.Run("NeighboringWorkerScript", func(t *testing.T) {
15101533
// Scenario: neighboring worker script
@@ -1640,6 +1663,7 @@ func TestSymlinkResolveRoot(t *testing.T) {
16401663
cwd, _ := os.Getwd()
16411664
testDir := filepath.Join(cwd, "..", "testdata", "symlinks", "test")
16421665
publicDir := filepath.Join(cwd, "..", "testdata", "symlinks", "public")
1666+
skipIfSymlinkNotValid(t, publicDir)
16431667

16441668
t.Run("ResolveRootSymlink", func(t *testing.T) {
16451669
// Tests that resolve_root_symlink directive works correctly
@@ -1698,6 +1722,7 @@ func TestSymlinkResolveRoot(t *testing.T) {
16981722
func TestSymlinkWorkerBehavior(t *testing.T) {
16991723
cwd, _ := os.Getwd()
17001724
publicDir := filepath.Join(cwd, "..", "testdata", "symlinks", "public")
1725+
skipIfSymlinkNotValid(t, publicDir)
17011726

17021727
t.Run("WorkerScriptFailsWithoutWorkerMode", func(t *testing.T) {
17031728
// Tests that accessing a worker-only script without configuring it as a worker actually results in an error

internal/fastabs/filepath.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ import (
99
// FastAbs can't be optimized on Windows because the
1010
// syscall.FullPath function takes an input.
1111
func FastAbs(path string) (string, error) {
12-
return filepath.Abs(path)
12+
// Normalize forward slashes to backslashes for Windows compatibility
13+
return filepath.Abs(filepath.FromSlash(path))
1314
}

testdata/files/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
http_response_code(404);

testdata/mercure-publish.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
require_once __DIR__.'/_executor.php';
44

55
return function () {
6-
echo "update 1: " . mercure_publish('foo', 'bar', true, 'myid', 'mytype', 10) . PHP_EOL;
7-
echo "update 2: " . mercure_publish(['baz', 'bar']) . PHP_EOL;
6+
echo "update 1: " . mercure_publish('foo', 'bar', true, 'myid', 'mytype', 10) . "\n";
7+
echo "update 2: " . mercure_publish(['baz', 'bar']) . "\n";
88
};

worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func newWorker(o workerOpt) (*worker, error) {
114114
// Order is important!
115115
// This order ensures that FrankenPHP started from inside a symlinked directory will properly resolve any paths.
116116
// If it is started from outside a symlinked directory, it is resolved to the same path that we use in the Caddy module.
117-
absFileName, err := filepath.EvalSymlinks(o.fileName)
117+
absFileName, err := filepath.EvalSymlinks(filepath.FromSlash(o.fileName))
118118
if err != nil {
119119
return nil, fmt.Errorf("worker filename is invalid %q: %w", o.fileName, err)
120120
}

0 commit comments

Comments
 (0)