@@ -20,6 +20,7 @@ import (
2020 "fmt"
2121 "io"
2222 "os"
23+ "path/filepath"
2324 "strings"
2425
2526 . "github.com/onsi/ginkgo/v2"
@@ -807,4 +808,214 @@ plugins:
807808 })
808809 })
809810 })
811+
812+ Context ("parseInputDirFromArgs" , func () {
813+ It ("should return empty when flag is not present" , func () {
814+ args := []string {"edit" , "--plugins=helm.kubebuilder.io/v2-alpha" }
815+ dir , found := parseInputDirFromArgs (args )
816+ Expect (found ).To (BeFalse ())
817+ Expect (dir ).To (BeEmpty ())
818+ })
819+
820+ It ("should parse --input-dir with = syntax" , func () {
821+ args := []string {"edit" , "--input-dir=/path/to/project" , "--plugins=helm.kubebuilder.io/v2-alpha" }
822+ dir , found := parseInputDirFromArgs (args )
823+ Expect (found ).To (BeTrue ())
824+ Expect (dir ).To (Equal ("/path/to/project" ))
825+ })
826+
827+ It ("should parse --input-dir with space syntax" , func () {
828+ args := []string {"edit" , "--input-dir" , "/path/to/project" , "--plugins=helm.kubebuilder.io/v2-alpha" }
829+ dir , found := parseInputDirFromArgs (args )
830+ Expect (found ).To (BeTrue ())
831+ Expect (dir ).To (Equal ("/path/to/project" ))
832+ })
833+
834+ It ("should return false when --input-dir has no value" , func () {
835+ args := []string {"edit" , "--input-dir" }
836+ dir , found := parseInputDirFromArgs (args )
837+ Expect (found ).To (BeFalse ())
838+ Expect (dir ).To (BeEmpty ())
839+ })
840+ })
841+
842+ Context ("isEditCommand" , func () {
843+ It ("should return true for edit command" , func () {
844+ args := []string {"edit" , "--plugins=helm.kubebuilder.io/v2-alpha" }
845+ Expect (isEditCommand (args )).To (BeTrue ())
846+ })
847+
848+ It ("should return true for edit command with flags before" , func () {
849+ args := []string {"--verbose" , "edit" , "--plugins=helm.kubebuilder.io/v2-alpha" }
850+ Expect (isEditCommand (args )).To (BeTrue ())
851+ })
852+
853+ It ("should return false for init command" , func () {
854+ args := []string {"init" , "--plugins=go.kubebuilder.io/v4" }
855+ Expect (isEditCommand (args )).To (BeFalse ())
856+ })
857+
858+ It ("should return false for create command" , func () {
859+ args := []string {"create" , "api" }
860+ Expect (isEditCommand (args )).To (BeFalse ())
861+ })
862+ })
863+
864+ Context ("handleInputDirBeforeConfigLoad integration" , func () {
865+ var (
866+ originalArgs []string
867+ originalDir string
868+ tmpDir string
869+ )
870+
871+ BeforeEach (func () {
872+ // Save original os.Args and working directory
873+ originalArgs = os .Args
874+ var err error
875+ originalDir , err = os .Getwd ()
876+ Expect (err ).NotTo (HaveOccurred ())
877+
878+ // Create temp directory for testing
879+ tmpDir , err = os .MkdirTemp ("" , "input-dir-integration-*" )
880+ Expect (err ).NotTo (HaveOccurred ())
881+ })
882+
883+ AfterEach (func () {
884+ // Restore original state
885+ os .Args = originalArgs
886+ if originalDir != "" {
887+ _ = os .Chdir (originalDir )
888+ }
889+ if tmpDir != "" {
890+ _ = os .RemoveAll (tmpDir )
891+ }
892+ })
893+
894+ It ("should do nothing when not an edit command" , func () {
895+ os .Args = []string {"kubebuilder" , "init" , "--plugins=go.kubebuilder.io/v4" }
896+ err := handleInputDirBeforeConfigLoad ()
897+ Expect (err ).NotTo (HaveOccurred ())
898+
899+ // Working directory should not have changed
900+ currentDir , err := os .Getwd ()
901+ Expect (err ).NotTo (HaveOccurred ())
902+ Expect (currentDir ).To (Equal (originalDir ))
903+ })
904+
905+ It ("should do nothing when input-dir is not specified" , func () {
906+ os .Args = []string {"kubebuilder" , "edit" , "--plugins=helm.kubebuilder.io/v2-alpha" }
907+ err := handleInputDirBeforeConfigLoad ()
908+ Expect (err ).NotTo (HaveOccurred ())
909+
910+ // Working directory should not have changed
911+ currentDir , err := os .Getwd ()
912+ Expect (err ).NotTo (HaveOccurred ())
913+ Expect (currentDir ).To (Equal (originalDir ))
914+ })
915+
916+ It ("should return error when input-dir does not exist" , func () {
917+ os .Args = []string {"kubebuilder" , "edit" , "--input-dir=/nonexistent/path" }
918+ err := handleInputDirBeforeConfigLoad ()
919+ Expect (err ).To (HaveOccurred ())
920+ Expect (err .Error ()).To (ContainSubstring ("does not exist" ))
921+ })
922+
923+ It ("should return error when input-dir is not a directory" , func () {
924+ // Create a file instead of directory
925+ filePath := filepath .Join (tmpDir , "testfile" )
926+ err := os .WriteFile (filePath , []byte ("test" ), 0o644 )
927+ Expect (err ).NotTo (HaveOccurred ())
928+
929+ os .Args = []string {"kubebuilder" , "edit" , "--input-dir=" + filePath }
930+ err = handleInputDirBeforeConfigLoad ()
931+ Expect (err ).To (HaveOccurred ())
932+ Expect (err .Error ()).To (ContainSubstring ("not a directory" ))
933+ })
934+
935+ It ("should return error when input-dir does not contain PROJECT file" , func () {
936+ // Create empty directory
937+ emptyDir := filepath .Join (tmpDir , "empty" )
938+ err := os .MkdirAll (emptyDir , 0o755 )
939+ Expect (err ).NotTo (HaveOccurred ())
940+
941+ os .Args = []string {"kubebuilder" , "edit" , "--input-dir=" + emptyDir }
942+ err = handleInputDirBeforeConfigLoad ()
943+ Expect (err ).To (HaveOccurred ())
944+ Expect (err .Error ()).To (ContainSubstring ("does not contain" ))
945+ Expect (err .Error ()).To (ContainSubstring ("PROJECT" ))
946+ })
947+
948+ It ("should change directory when valid input-dir is provided" , func () {
949+ // Create project directory with PROJECT file
950+ projectDir := filepath .Join (tmpDir , "project" )
951+ err := os .MkdirAll (projectDir , 0o755 )
952+ Expect (err ).NotTo (HaveOccurred ())
953+
954+ projectFile := filepath .Join (projectDir , "PROJECT" )
955+ projectContent := `domain: example.com
956+ layout:
957+ - go.kubebuilder.io/v4
958+ - helm.kubebuilder.io/v2-alpha
959+ projectName: test-project
960+ repo: example.com/test-project
961+ version: "3"
962+ `
963+ err = os .WriteFile (projectFile , []byte (projectContent ), 0o644 )
964+ Expect (err ).NotTo (HaveOccurred ())
965+
966+ os .Args = []string {"kubebuilder" , "edit" , "--input-dir=" + projectDir }
967+ err = handleInputDirBeforeConfigLoad ()
968+ Expect (err ).NotTo (HaveOccurred ())
969+
970+ // Verify directory changed
971+ currentDir , err := os .Getwd ()
972+ Expect (err ).NotTo (HaveOccurred ())
973+
974+ // Resolve symlinks for comparison
975+ expectedDir , err := filepath .EvalSymlinks (projectDir )
976+ Expect (err ).NotTo (HaveOccurred ())
977+ actualDir , err := filepath .EvalSymlinks (currentDir )
978+ Expect (err ).NotTo (HaveOccurred ())
979+
980+ Expect (actualDir ).To (Equal (expectedDir ))
981+ })
982+
983+ It ("should handle relative paths correctly" , func () {
984+ // Create project directory with PROJECT file
985+ projectDir := filepath .Join (tmpDir , "project" )
986+ err := os .MkdirAll (projectDir , 0o755 )
987+ Expect (err ).NotTo (HaveOccurred ())
988+
989+ projectFile := filepath .Join (projectDir , "PROJECT" )
990+ projectContent := `domain: example.com
991+ layout:
992+ - go.kubebuilder.io/v4
993+ projectName: test-project
994+ repo: example.com/test-project
995+ version: "3"
996+ `
997+ err = os .WriteFile (projectFile , []byte (projectContent ), 0o644 )
998+ Expect (err ).NotTo (HaveOccurred ())
999+
1000+ // Change to tmpDir and use relative path
1001+ err = os .Chdir (tmpDir )
1002+ Expect (err ).NotTo (HaveOccurred ())
1003+
1004+ os .Args = []string {"kubebuilder" , "edit" , "--input-dir=./project" }
1005+ err = handleInputDirBeforeConfigLoad ()
1006+ Expect (err ).NotTo (HaveOccurred ())
1007+
1008+ // Verify directory changed
1009+ currentDir , err := os .Getwd ()
1010+ Expect (err ).NotTo (HaveOccurred ())
1011+
1012+ // Resolve symlinks for comparison
1013+ expectedDir , err := filepath .EvalSymlinks (projectDir )
1014+ Expect (err ).NotTo (HaveOccurred ())
1015+ actualDir , err := filepath .EvalSymlinks (currentDir )
1016+ Expect (err ).NotTo (HaveOccurred ())
1017+
1018+ Expect (actualDir ).To (Equal (expectedDir ))
1019+ })
1020+ })
8101021})
0 commit comments