From 63703bc437a726c73aacc1f28b0f16a4423bd77c Mon Sep 17 00:00:00 2001 From: Javier Alvarez Garcia Date: Tue, 7 Feb 2023 10:41:11 +0100 Subject: [PATCH] Add interface to get run and test deps Since tests are identified as a single command, dependencies for tests and run are implicitly assumed to be the same as for the Build step, including the target binary. This is, however not correct because it doesn't account for the fact that we may want to use a different test runner or some additional target that must be compiled before testing, but not necessarily in the build step of the target. With this purpose this change introduces an extendedTestInterface and extendedRunInteface where the user may also specify a TestDeps() or RunDeps() function to get the dependencies of the test/run step. --- RULES/core/context.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/RULES/core/context.go b/RULES/core/context.go index edddf4f..2ecc227 100644 --- a/RULES/core/context.go +++ b/RULES/core/context.go @@ -96,10 +96,24 @@ type runInterface interface { Run(args []string) string } +// An interface for runnables that depend on some other set of targets to run, but not to build +// For example, when using a test wrapper. +type extendedRunInterface interface { + runInterface + RunDeps() []OutPath +} + type testInterface interface { Test(args []string) string } +// An interface for tests that depend on some other set of targets to run, but not to build +// For example, when using a test wrapper. +type extendedTestInterface interface { + testInterface + TestDeps() []OutPath +} + type CoverageInterface interface { Test(args []string) string Binaries() []Path @@ -314,9 +328,17 @@ func (ctx *context) handleTarget(targetPath string, target buildInterface) { }) if runIface, ok := target.(runInterface); ok { + deps := []string{} + if extendedRunIface, ok := target.(extendedRunInterface); ok { + depsPaths := extendedRunIface.RunDeps() + for _, dep := range depsPaths { + deps = append(deps, dep.Absolute()) + } + } + ctx.targetRules = append(ctx.targetRules, TargetRule{ Target: fmt.Sprintf("%s#run", targetPath), - Ins: []string{targetPath}, + Ins: append(deps, targetPath), Variables: map[string]string{ "command": runIface.Run(input.RunArgs), "description": fmt.Sprintf("Running %s:", targetPath), @@ -326,9 +348,17 @@ func (ctx *context) handleTarget(targetPath string, target buildInterface) { } if testIface, ok := target.(testInterface); ok { + deps := []string{} + if extendedTestIface, ok := target.(extendedTestInterface); ok { + depsPaths := extendedTestIface.TestDeps() + for _, dep := range depsPaths { + deps = append(deps, dep.Absolute()) + } + } + ctx.targetRules = append(ctx.targetRules, TargetRule{ Target: fmt.Sprintf("%s#test", targetPath), - Ins: []string{targetPath}, + Ins: append(deps, targetPath), Variables: map[string]string{ "command": testIface.Test(input.TestArgs), "description": fmt.Sprintf("Testing %s:", targetPath),