From 8f09ed8f0dba3b3f9ad6d0a8428c11290f9a2b90 Mon Sep 17 00:00:00 2001
From: Simon Lightfoot <simon@devangels.london>
Date: Mon, 1 May 2017 03:15:59 +0100
Subject: [PATCH] Bugfix for issue #1628 where `Caddyfile` is not being hidden
 correctly on windows.

Added test case to check if Caddyfile is added to HiddenFiles correctly.
---
 caddyhttp/httpserver/plugin.go      |  2 +-
 caddyhttp/httpserver/plugin_test.go | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/caddyhttp/httpserver/plugin.go b/caddyhttp/httpserver/plugin.go
index b6fa575e7..8d89ccb30 100644
--- a/caddyhttp/httpserver/plugin.go
+++ b/caddyhttp/httpserver/plugin.go
@@ -70,7 +70,7 @@ func hideCaddyfile(cctx caddy.Context) error {
 			return err
 		}
 		if strings.HasPrefix(absOriginCaddyfile, absRoot) {
-			cfg.HiddenFiles = append(cfg.HiddenFiles, strings.TrimPrefix(absOriginCaddyfile, absRoot))
+			cfg.HiddenFiles = append(cfg.HiddenFiles, filepath.ToSlash(strings.TrimPrefix(absOriginCaddyfile, absRoot)))
 		}
 	}
 	return nil
diff --git a/caddyhttp/httpserver/plugin_test.go b/caddyhttp/httpserver/plugin_test.go
index 49bb54e67..347813884 100644
--- a/caddyhttp/httpserver/plugin_test.go
+++ b/caddyhttp/httpserver/plugin_test.go
@@ -209,3 +209,27 @@ func TestContextSaveConfig(t *testing.T) {
 		t.Errorf("Expected len(siteConfigs) == %d, but was %d", want, got)
 	}
 }
+
+// Test to make sure we are correctly hiding the Caddyfile
+func TestHideCaddyfile(t *testing.T) {
+	ctx := newContext().(*httpContext)
+	ctx.saveConfig("test", &SiteConfig{
+		Root:            Root,
+		originCaddyfile: "Testfile",
+	})
+	err := hideCaddyfile(ctx)
+	if err != nil {
+		t.Fatalf("Failed to hide Caddyfile, got: %v", err)
+		return
+	}
+	if len(ctx.siteConfigs[0].HiddenFiles) == 0 {
+		t.Fatal("Failed to add Caddyfile to HiddenFiles.")
+		return
+	}
+	for _, file := range ctx.siteConfigs[0].HiddenFiles {
+		if file == "/Testfile" {
+			return
+		}
+	}
+	t.Fatal("Caddyfile missing from HiddenFiles")
+}