@@ -502,3 +502,111 @@ func TestParityGoldenHelpStructure(t *testing.T) {
502502 }
503503 }
504504}
505+
506+ // --- apm init command parity tests ---
507+
508+ // TestParityInitCreatesApmYML verifies that `apm init --yes` creates apm.yml
509+ // in a fresh directory with the expected YAML keys.
510+ func TestParityInitCreatesApmYML (t * testing.T ) {
511+ if goBinPath == "" {
512+ t .Skip ("Go binary not built; skipping" )
513+ }
514+ dir := t .TempDir ()
515+ stdout , stderr , code := runGoInDir (t , dir , "init" , "--yes" )
516+ if code != 0 {
517+ t .Fatalf ("apm init --yes exited %d\n stdout: %s\n stderr: %s" , code , stdout , stderr )
518+ }
519+
520+ data , err := os .ReadFile (filepath .Join (dir , "apm.yml" ))
521+ if err != nil {
522+ t .Fatalf ("apm.yml not created: %v" , err )
523+ }
524+ content := string (data )
525+ for _ , key := range []string {"name:" , "version:" , "description:" , "author:" , "dependencies:" } {
526+ if ! strings .Contains (content , key ) {
527+ t .Errorf ("apm.yml missing key %q\n Content:\n %s" , key , content )
528+ }
529+ }
530+ }
531+
532+ // TestParityInitExitCode verifies `apm init --yes` exits 0.
533+ func TestParityInitExitCode (t * testing.T ) {
534+ if goBinPath == "" {
535+ t .Skip ("Go binary not built; skipping" )
536+ }
537+ dir := t .TempDir ()
538+ _ , _ , code := runGoInDir (t , dir , "init" , "--yes" )
539+ if code != 0 {
540+ t .Errorf ("apm init --yes exit code = %d, want 0" , code )
541+ }
542+ }
543+
544+ // TestParityInitIdempotent verifies `apm init --yes` succeeds when apm.yml already exists.
545+ func TestParityInitIdempotent (t * testing.T ) {
546+ if goBinPath == "" {
547+ t .Skip ("Go binary not built; skipping" )
548+ }
549+ dir := t .TempDir ()
550+ // First run.
551+ _ , _ , code := runGoInDir (t , dir , "init" , "--yes" )
552+ if code != 0 {
553+ t .Fatalf ("first apm init --yes exited %d" , code )
554+ }
555+ // Second run: should succeed (not error on existing apm.yml).
556+ _ , _ , code2 := runGoInDir (t , dir , "init" , "--yes" )
557+ if code2 != 0 {
558+ t .Errorf ("second apm init --yes (idempotent) exited %d, want 0" , code2 )
559+ }
560+ }
561+
562+ // TestParityInitProjectName verifies `apm init --yes myproject` creates a subdir.
563+ func TestParityInitProjectName (t * testing.T ) {
564+ if goBinPath == "" {
565+ t .Skip ("Go binary not built; skipping" )
566+ }
567+ dir := t .TempDir ()
568+ stdout , stderr , code := runGoInDir (t , dir , "init" , "--yes" , "myproject" )
569+ if code != 0 {
570+ t .Fatalf ("apm init --yes myproject exited %d\n stdout: %s\n stderr: %s" , code , stdout , stderr )
571+ }
572+ if _ , err := os .Stat (filepath .Join (dir , "myproject" , "apm.yml" )); err != nil {
573+ t .Errorf ("myproject/apm.yml not created: %v" , err )
574+ }
575+ }
576+
577+ // TestParityInitOutputContainsSuccess verifies the success message is printed.
578+ func TestParityInitOutputContainsSuccess (t * testing.T ) {
579+ if goBinPath == "" {
580+ t .Skip ("Go binary not built; skipping" )
581+ }
582+ dir := t .TempDir ()
583+ stdout , _ , code := runGoInDir (t , dir , "init" , "--yes" )
584+ if code != 0 {
585+ t .Fatalf ("apm init --yes exited %d" , code )
586+ }
587+ if ! strings .Contains (stdout , "initialized" ) && ! strings .Contains (stdout , "apm.yml" ) {
588+ t .Errorf ("expected success output, got: %q" , stdout )
589+ }
590+ }
591+
592+ // runGoInDir executes the Go binary from a given working directory.
593+ func runGoInDir (t * testing.T , dir string , args ... string ) (stdout , stderr string , exitCode int ) {
594+ t .Helper ()
595+ if goBinPath == "" {
596+ t .Skip ("Go binary not built; skipping" )
597+ }
598+ var outBuf , errBuf bytes.Buffer
599+ cmd := exec .Command (goBinPath , args ... )
600+ cmd .Dir = dir
601+ cmd .Stdout = & outBuf
602+ cmd .Stderr = & errBuf
603+ err := cmd .Run ()
604+ if err != nil {
605+ if exitErr , ok := err .(* exec.ExitError ); ok {
606+ exitCode = exitErr .ExitCode ()
607+ } else {
608+ exitCode = - 1
609+ }
610+ }
611+ return outBuf .String (), errBuf .String (), exitCode
612+ }
0 commit comments