用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/mongodb/mongo-tools --skill mongo-tools-js-to-go命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | mongo-tools-js-to-go |
| description | Use when converting JS/resmoke integration tests in mongo-tools to Go testify tests |
For tests within an individual tool's package (e.g. mongoimport/, mongodump/):
func TestFoo(t *testing.T) {
testtype.SkipUnlessTestType(t, testtype.IntegrationTestType)
const (
dbName = "mongofoo_test_db"
collName = "coll"
)
sessionProvider, _, err := testutil.GetBareSessionProvider()
require.NoError(t, err)
client, err := sessionProvider.GetSession()
require.NoError(t, err)
t.Cleanup(func() {
_ = client.Database(dbName).Drop(context.Background())
})
coll := client.Database(dbName).Collection(collName)
ns := &options.Namespace{DB: dbName, Collection: collName}
// ...
}
Test type constants: IntegrationTestType, ReplSetTestType, ShardedIntegrationTestType,
SSLTestType, AuthTestType.
Tests that exercise the full tool pipeline (dump+restore or export+import) belong in
integration/dumprestore or integration/exportimport and use testify suites.
Add tests as methods on the existing suite type for the relevant package. The suite entry point looks like:
type DumpRestoreSuite struct {
integrationSuite.IntegrationSuite
}
func TestDumpRestore(t *testing.T) {
testtype.SkipUnlessTestType(t, testtype.IntegrationTestType)
suite.Run(t, new(DumpRestoreSuite))
}
Suite test methods:
func (s *DumpRestoreSuite) TestFoo() {
ctx := s.Context()
client := s.Client()
dbName := s.DBName()
// use s.Require() / s.Assert() instead of require.New(t)
}
Key suite methods:
| Method | Purpose |
|---|---|
s.Context() | Test-scoped context |
s.Client() | New MongoDB client (caller responsible for Disconnect) |
s.DBName(prefix...) | DB name derived from test name, truncated to 63 chars |
s.Require() | testify require bound to current (sub)test |
s.Assert() | testify assert bound to current (sub)test |
s.T() | Current *testing.T |
s.Run(name, func()) | Subtest (updates s.T() for the duration) |
No manual DB cleanup needed — BeforeTest in IntegrationSuite drops all non-system databases
before each test method. Do not register t.Cleanup DB drops in suite tests.
Callers before callees: test functions before helpers, helpers before the helpers they call. A
helper used by one test goes immediately below that test, not at the bottom of the file. A helper
that creates or sets up collections for several tests goes in suite_test.go with the existing
create-collection helpers, not in the test file.
No comments that describe what code is doing — use named functions, subtests, and descriptive variable names instead. Comments explaining why are fine.
Use any not interface{}
Use bson.D — not bson.M — for documents, filters, and commands, matching the rest of the
codebase. Build bson.D/bson.E literals unkeyed.
Use for i := range n, not for i := 0; i < n; i++.
Use a set for membership checks, rather than scanning a slice. Sets come from
mapset "github.com/deckarep/golang-set/v2" (aliased on import, as the rest of the codebase does)
— not a map[string]struct{}:
var systemDBs = mapset.NewSet("admin", "local", "config")
if systemDBs.Contains(name) { ... }
Always include assertion messages:
assert.Equal(t, want, got, "description of what is being tested")
Reset map[string]any{} before each Decode call — stale keys from previous decodes persist
otherwise
Table-driven tests: define a type fooCase struct and loop over []fooCase
For error cases, don't use require.Error Use one of the following:
require.ErrorIs(t, err, something)require.ErrorAs(t, err, &var)require.ErrorContains(t, err, "substring")Reviewers raise these on nearly every conversion PR. Get them right the first time.
const used inside exactly one function belongs inside
that function. Package-level constants are for values several functions share.const fooDocCount = 7 invites a reviewer to
hunt for the significance of 7 and find none. Constants are worth naming when the value is
load-bearing (a size limit, a timeout the tool cares about).assertFooRestored that only counts documents is
misnamed, and a new restoreFromArgs sitting next to an existing getRestoreWithArgs that does
something different will confuse everyone. Check for an existing helper with a similar name before
adding one."drop" or "shard" read as
meaningful and send reviewers looking for behavior that isn't there. Use obviously inert values.s.Run/t.Run when the cases
share setup or form a table, not to group otherwise independent tests.Reviewers have asked for this repeatedly. A comment saying "this converts foo.js", "the JS test's
intent was...", or otherwise narrating the provenance of a test is not useful to anyone reading the
Go code later. Delete them.
Provenance belongs in the commit message, which is also the PR description: name the JS files the commit converts and deletes there.
The one comment worth keeping in the code is a coverage note: when the Go test deliberately does not cover something the JS test did, say so where the test is, because that is a fact about the current test rather than a fact about history.
The reviewer reads the deleted JS alongside the new Go, so any divergence gets noticed.
v
field, or dumps from one cluster and restores to another, either reproduce it or call it out
explicitly in the PR description as coverage not carried over. Both of those have been caught in
review.integration/dumprestore and looks like the tests already there, even if the JS
version lived somewhere else.A reviewer has told us directly that these PRs are hard to review, mostly because of size — one was nearly 2,000 lines. Reviewing a conversion means reading the deleted JS carefully and the new Go carefully.
AGENTS.md says. Split by test file or by theme rather than
converting a whole directory at once.Round-trip tests belong in integration/exportimport or integration/dumprestore (suite methods),
not in the individual tool packages.
Critical: drop the collection between export/dump and import/restore. Without this, the restore can't be verified.
_, err = me.Export(tmpFile)
s.Require().NoError(err)
s.Require().NoError(tmpFile.Close())
s.Require().NoError(coll.Drop(s.Context())) // ← required
// now import and verify
Use Go data structures + json.Marshal (not hardcoded strings):
upsertFile := writeJSONLinesFile(t, dir, "data.json", []map[string]any{
{"_id": "one", "a": 1234, "b": "foo"},
{"_id": "two", "a": "xxx", "b": "yyy"},
})
For BSON-type-preserving output (e.g. subdocument _ids), use
bson.MarshalExtJSON(doc, relaxed, escapeHTML).
| Helper | Purpose |
|---|---|
testutil.GetBareSessionProvider() | Get a live MongoDB client |
testutil.GetToolOptions() | Get tool options (connects to test mongod) |
testutil.GetBareArgs() | CLI args (--host, --port, auth) for exec.Command |
runImportOpts(t, ns, file, IngestOptions{}) | Import, returns errors from New() too (use for option-validation tests) |
importWithIngestOpts(t, ns, file, IngestOptions{}) | Import, fails test if New() errors |
testutil.AssertBrokenPipeHandled(t, cmd) | Verify a process handles SIGPIPE as a write error |
Tool-package test:
TOOLS_TESTING_INTEGRATION=true \
go test ./mongoimport/... -v -run TestFoo -count=1
Suite (e2e) test:
TOOLS_TESTING_INTEGRATION=true \
go test ./integration/dumprestore/... -v -run TestDumpRestore/TestFoo -count=1
Add TOOLS_TESTING_AUTH=1 TOOLS_TESTING_AUTH_USERNAME=... TOOLS_TESTING_AUTH_PASSWORD=... when
testing against an auth-enabled mongod.
Always run the test locally before committing.
integration/dumprestore or integration/exportimportfunc TestFoo(t *testing.T) inside the tool's own packagetestify.TOOLS-1234. Ask the user which
ticket to use if you don't know which one is being used for this work.precious tidy -gprecious lint -gbson.E struct literal uses unkeyed fields — suppressed by .golangci.yml, ignore itprecious tidy -g fixes indentation and line-length issues automatically