From 3ca60a9f0e046622194cd0b06aab7624329bbf26 Mon Sep 17 00:00:00 2001 From: Gauravms2143 Date: Wed, 24 May 2023 17:28:56 +0530 Subject: [PATCH] Enhancement: Create the imposter folder when the mock server is starting up. --- internal/server/http/server.go | 9 ++++++++- internal/server/http/server_test.go | 2 -- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/server/http/server.go b/internal/server/http/server.go index f34d438..3b81b58 100644 --- a/internal/server/http/server.go +++ b/internal/server/http/server.go @@ -90,7 +90,14 @@ func (s *Server) Build() error { } if _, err := os.Stat(s.impostersPath); os.IsNotExist(err) { - return fmt.Errorf("%w: the directory %s doesn't exists", err, s.impostersPath) + // If imposter folder is not there create it. + dirName := s.impostersPath + + err := os.Mkdir(dirName, 0755) + if err != nil { + fmt.Println(err) + } + log.Println("imposters directory created successfully.") } var impostersCh = make(chan []Imposter) var done = make(chan struct{}) diff --git a/internal/server/http/server_test.go b/internal/server/http/server_test.go index e083af5..0367cdf 100644 --- a/internal/server/http/server_test.go +++ b/internal/server/http/server_test.go @@ -2,7 +2,6 @@ package http import ( "crypto/tls" - "errors" "io" "io/ioutil" "log" @@ -31,7 +30,6 @@ func TestServer_Build(t *testing.T) { server Server err error }{ - {"imposter directory not found", NewServer("failImposterPath", nil, &http.Server{}, &Proxy{}, false, imposterFs), errors.New("hello")}, {"malformatted json", NewServer("test/testdata/malformatted_imposters", nil, &http.Server{}, &Proxy{}, false, imposterFs), nil}, {"valid imposter", NewServer("test/testdata/imposters", mux.NewRouter(), &http.Server{}, &Proxy{}, false, imposterFs), nil}, }