@@ -606,3 +606,42 @@ func TestExportPretty(t *testing.T) {
606606 assert .Equal (t , float64 (2 ), docs [2 ]["b" ], "docs[2].b should be 2" )
607607 assert .Equal (t , float64 (3 ), docs [2 ]["c" ], "docs[2].c should be 3" )
608608}
609+
610+ // TestExportWritesToStdout verifies that the mongoexport CLI writes all
611+ // documents to stdout when no --out flag is given.
612+ func TestExportWritesToStdout (t * testing.T ) {
613+ testtype .SkipUnlessTestType (t , testtype .IntegrationTestType )
614+ log .SetWriter (io .Discard )
615+
616+ const dbName = "mongoexport_stdout_test"
617+ const collName = "data"
618+
619+ sessionProvider , _ , err := testutil .GetBareSessionProvider ()
620+ require .NoError (t , err )
621+ client , err := sessionProvider .GetSession ()
622+ require .NoError (t , err )
623+ t .Cleanup (func () {
624+ if err := client .Database (dbName ).Drop (context .Background ()); err != nil {
625+ t .Errorf ("dropping test database: %v" , err )
626+ }
627+ })
628+
629+ docs := make ([]any , 20 )
630+ for i := range 20 {
631+ docs [i ] = bson.D {{"_id" , int32 (i )}}
632+ }
633+ _ , err = client .Database (dbName ).Collection (collName ).InsertMany (t .Context (), docs )
634+ require .NoError (t , err )
635+
636+ args := []string {"go" , "run" , "./main" }
637+ args = append (args , testutil .GetBareArgs ()... )
638+ args = append (args , "--db" , dbName , "--collection" , collName )
639+ cmd := exec .Command (args [0 ], args [1 :]... )
640+ stdout , err := cmd .Output ()
641+ require .NoError (t , err , "mongoexport should exit 0 when writing to stdout" )
642+
643+ output := string (stdout )
644+ for i := range 20 {
645+ assert .Contains (t , output , fmt .Sprintf (`"_id":%d` , i ), "stdout should contain _id=%d" , i )
646+ }
647+ }
0 commit comments