@@ -15,6 +15,7 @@ import (
1515 "fmt"
1616 "io"
1717 "os"
18+ "os/exec"
1819 "runtime"
1920 "strings"
2021 "testing"
@@ -571,3 +572,42 @@ func TestExportPretty(t *testing.T) {
571572 assert .Equal (t , float64 (2 ), docs [2 ]["b" ], "docs[2].b should be 2" )
572573 assert .Equal (t , float64 (3 ), docs [2 ]["c" ], "docs[2].c should be 3" )
573574}
575+
576+ // TestExportWritesToStdout verifies that the mongoexport CLI writes all
577+ // documents to stdout when no --out flag is given (from stdout.js).
578+ func TestExportWritesToStdout (t * testing.T ) {
579+ testtype .SkipUnlessTestType (t , testtype .IntegrationTestType )
580+ log .SetWriter (io .Discard )
581+
582+ const dbName = "mongoexport_stdout_test"
583+ const collName = "data"
584+
585+ sessionProvider , _ , err := testutil .GetBareSessionProvider ()
586+ require .NoError (t , err )
587+ client , err := sessionProvider .GetSession ()
588+ require .NoError (t , err )
589+ t .Cleanup (func () {
590+ if err := client .Database (dbName ).Drop (context .Background ()); err != nil {
591+ t .Errorf ("dropping test database: %v" , err )
592+ }
593+ })
594+
595+ docs := make ([]any , 20 )
596+ for i := range 20 {
597+ docs [i ] = bson.D {{"_id" , int32 (i )}}
598+ }
599+ _ , err = client .Database (dbName ).Collection (collName ).InsertMany (t .Context (), docs )
600+ require .NoError (t , err )
601+
602+ args := []string {"go" , "run" , "./main" }
603+ args = append (args , testutil .GetBareArgs ()... )
604+ args = append (args , "--db" , dbName , "--collection" , collName )
605+ cmd := exec .Command (args [0 ], args [1 :]... )
606+ stdout , err := cmd .Output ()
607+ require .NoError (t , err , "mongoexport should exit 0 when writing to stdout" )
608+
609+ output := string (stdout )
610+ for i := range 20 {
611+ assert .Contains (t , output , fmt .Sprintf (`"_id":%d` , i ), "stdout should contain _id=%d" , i )
612+ }
613+ }
0 commit comments