Skip to content

Commit 0e9ff5c

Browse files
committed
Convert test/qa-tests/jstests/export/stdout.js to Go
1 parent 17dfb1c commit 0e9ff5c

2 files changed

Lines changed: 39 additions & 56 deletions

File tree

mongoexport/mongoexport_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

test/qa-tests/jstests/export/stdout.js

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)