@@ -489,7 +489,7 @@ func TestBrokenPipe(t *testing.T) {
489489}
490490
491491// TestExportNamespaceValidation verifies that mongoexport rejects invalid DB
492- // names and accepts system collections (from namespace_validation.js) .
492+ // names and accepts system collections.
493493func TestExportNamespaceValidation (t * testing.T ) {
494494 testtype .SkipUnlessTestType (t , testtype .IntegrationTestType )
495495 log .SetWriter (io .Discard )
@@ -519,3 +519,35 @@ func TestExportNamespaceValidation(t *testing.T) {
519519 me .Close ()
520520 }
521521}
522+
523+ // TestExportNoData verifies that exporting an empty collection succeeds, but
524+ // fails with --assertExists.
525+ func TestExportNoData (t * testing.T ) {
526+ testtype .SkipUnlessTestType (t , testtype .IntegrationTestType )
527+ log .SetWriter (io .Discard )
528+
529+ toolOptions , err := testutil .GetToolOptions ()
530+ require .NoError (t , err )
531+ toolOptions .Namespace = & options.Namespace {DB : "test" , Collection : "mongoexport_no_data_test" }
532+
533+ me , err := New (Options {
534+ ToolOptions : toolOptions ,
535+ OutputFormatOptions : & OutputFormatOptions {Type : "json" , JSONFormat : "canonical" },
536+ InputOptions : & InputOptions {},
537+ })
538+ require .NoError (t , err )
539+ defer me .Close ()
540+ var buf bytes.Buffer
541+ _ , err = me .Export (& buf )
542+ assert .NoError (t , err , "export from empty collection should succeed" )
543+
544+ me2 , err := New (Options {
545+ ToolOptions : toolOptions ,
546+ OutputFormatOptions : & OutputFormatOptions {Type : "json" , JSONFormat : "canonical" },
547+ InputOptions : & InputOptions {AssertExists : true },
548+ })
549+ require .NoError (t , err )
550+ defer me2 .Close ()
551+ _ , err = me2 .Export (& buf )
552+ assert .Error (t , err , "export with --assertExists should fail on nonexistent collection" )
553+ }
0 commit comments