@@ -6,7 +6,10 @@ package middleware
66import (
77 "context"
88 "errors"
9+ "os"
910 osexec "os/exec"
11+ "path/filepath"
12+ "runtime"
1013 "strings"
1114 "testing"
1215
@@ -288,6 +291,81 @@ func Test_ServiceHooks_Registered(t *testing.T) {
288291 require .Equal (t , 1 , preDeployCount )
289292}
290293
294+ func Test_ServiceHooks_ValidationUsesServicePath (t * testing.T ) {
295+ mockContext := mocks .NewMockContext (context .Background ())
296+ azdContext := createAzdContext (t )
297+
298+ envName := "test"
299+ runOptions := Options {CommandPath : "deploy" }
300+
301+ projectConfig := project.ProjectConfig {
302+ Name : envName ,
303+ Services : map [string ]* project.ServiceConfig {},
304+ }
305+
306+ hookPath := filepath .Join ("scripts" , "predeploy.ps1" )
307+ expectedShell := "pwsh"
308+ scriptContents := "Write-Host 'Hello'\n "
309+ if runtime .GOOS == "windows" {
310+ hookPath = filepath .Join ("scripts" , "predeploy.sh" )
311+ expectedShell = "bash"
312+ scriptContents = "echo hello\n "
313+ }
314+
315+ serviceConfig := & project.ServiceConfig {
316+ EventDispatcher : ext .NewEventDispatcher [project.ServiceLifecycleEventArgs ](project .ServiceEvents ... ),
317+ Language : "ts" ,
318+ RelativePath : "./src/api" ,
319+ Host : "appservice" ,
320+ Hooks : map [string ][]* ext.HookConfig {
321+ "predeploy" : {
322+ {
323+ Run : hookPath ,
324+ },
325+ },
326+ },
327+ }
328+
329+ projectConfig .Services ["api" ] = serviceConfig
330+
331+ err := ensureAzdValid (mockContext , azdContext , envName , & projectConfig )
332+ require .NoError (t , err )
333+
334+ projectConfig .Services ["api" ].Project = & projectConfig
335+
336+ serviceHookPath := filepath .Join (serviceConfig .Path (), hookPath )
337+ require .NoError (t , os .MkdirAll (filepath .Dir (serviceHookPath ), 0o755 ))
338+ require .NoError (t , os .WriteFile (serviceHookPath , []byte (scriptContents ), 0o600 ))
339+
340+ mockContext .CommandRunner .MockToolInPath ("pwsh" , nil )
341+
342+ var executedShell string
343+ mockContext .CommandRunner .When (func (args exec.RunArgs , command string ) bool {
344+ return true
345+ }).RespondFn (func (args exec.RunArgs ) (exec.RunResult , error ) {
346+ executedShell = args .Cmd
347+ return exec .NewRunResult (0 , "" , "" ), nil
348+ })
349+
350+ nextFn := func (ctx context.Context ) (* actions.ActionResult , error ) {
351+ err := serviceConfig .Invoke (ctx , project .ServiceEventDeploy , project.ServiceLifecycleEventArgs {
352+ Project : & projectConfig ,
353+ Service : serviceConfig ,
354+ ServiceContext : project .NewServiceContext (),
355+ }, func () error {
356+ return nil
357+ })
358+
359+ return & actions.ActionResult {}, err
360+ }
361+
362+ result , err := runMiddleware (mockContext , envName , & projectConfig , & runOptions , nextFn )
363+
364+ require .NotNil (t , result )
365+ require .NoError (t , err )
366+ require .Equal (t , expectedShell , executedShell )
367+ }
368+
291369func createAzdContext (t * testing.T ) * azdcontext.AzdContext {
292370 tempDir := t .TempDir ()
293371 ostest .Chdir (t , tempDir )
0 commit comments