fimplemented smart builder/container auto-selection

automatically handles pack->container=true and container=false->host builder. also updated the e2e test and added new smartbuilderselection test in run_test.go
This commit is contained in:
RayyanSeliya 2025-08-06 23:55:40 +05:30
parent 4b2b46cbc0
commit 6e5b4294f0
3 changed files with 44 additions and 2 deletions

View File

@ -177,11 +177,18 @@ func runRun(cmd *cobra.Command, newClient ClientFactory) (err error) {
return
}
// Auto-fix: Force container=true for containerized builders when --container flag not explicitly set
// Smart auto-fix logic for builder/container compatibility
// This fixes the original bug where --builder=pack doesn't default to container=true
// Case 1: Containerized builders (pack/s2i) should force container=true when not explicitly set
if (f.Build.Builder == "pack" || f.Build.Builder == "s2i") && !cfg.Container && !cmd.Flags().Changed("container") {
cfg.Container = true
}
// Case 2: container=false should auto-select host builder when no builder explicitly set
if !cfg.Container && cmd.Flags().Changed("container") && !cmd.Flags().Changed("builder") {
f.Build.Builder = "host"
}
// Validate after configure and auto-fix
if err = cfg.Validate(cmd, f); err != nil {

View File

@ -673,3 +673,38 @@ func TestCtrBuilderConflict(t *testing.T) {
t.Error("error expected but got <nil>")
}
}
// TestSmartBuilderSelection tests that container=false auto-selects host builder
func TestSmartBuilderSelection(t *testing.T) {
var runner = mock.NewRunner()
var builder = mock.NewBuilder()
tmp := t.TempDir()
fnPath := filepath.Join(tmp, "fn")
f, err := fn.New().Init(fn.Function{Root: fnPath, Runtime: "go"})
if err != nil {
t.Fatal(err)
}
err = f.Write()
if err != nil {
t.Fatal(err)
}
t.Chdir(fnPath)
cmd := NewRunCmd(NewTestClient(
fn.WithRunner(runner),
fn.WithBuilder(builder),
fn.WithRegistry(TestRegistry),
))
cmd.SetArgs([]string{"--container=false", "--build=1"})
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(time.Second, func() {
cancel()
})
err = cmd.ExecuteContext(ctx)
if err != nil {
t.Fatal(err)
}
}

View File

@ -28,7 +28,7 @@ func TestFunctionRunWithoutContainer(t *testing.T) {
var funcPath = filepath.Join(t.TempDir(), funcName)
knFuncTerm1 := common.NewKnFuncShellCli(t)
knFuncTerm1.Exec("create", "--language", "go", "--template", "http", "--builder", "host", funcPath)
knFuncTerm1.Exec("create", "--language", "go", "--template", "http", funcPath)
knFuncTerm1.ShouldDumpOnSuccess = false
knFuncTerm2 := common.NewKnFuncShellCli(t)