test(docs): compile the sdk.md examples by ndeloof · Pull Request #14136 · docker/compose · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions docs/examples/sdk/main.go
44 changes: 44 additions & 0 deletions docs/examples/sdk/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2020 Docker Compose CLI authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"bytes"
"os"

"github.com/docker/cli/cli/command"

"github.com/docker/compose/v5/pkg/api"
"github.com/docker/compose/v5/pkg/compose"
)

// customService demonstrates the options accepted by NewComposeService: the
// second fenced Go block of docs/sdk.md is this function's body, verbatim
// (pinned by sdk_md_test.go).
func customService(dockerCLI command.Cli) (api.Compose, error) {
// Create a custom output buffer to capture logs
var outputBuffer bytes.Buffer

// Create a compose service with custom options
service, err := compose.NewComposeService(dockerCLI,
compose.WithOutputStream(&outputBuffer), // Redirect output to custom writer
compose.WithErrorStream(os.Stderr), // Use stderr for errors
compose.WithMaxConcurrency(4), // Limit concurrent operations
compose.WithPrompt(compose.AlwaysOkPrompt()), // Auto-confirm all prompts
)
return service, err
}
57 changes: 57 additions & 0 deletions docs/examples/sdk/sdk_md_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2020 Docker Compose CLI authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"os"
"regexp"
"strings"
"testing"

"gotest.tools/v3/assert"
)

// the compiler keeps the example alive; the doc keeps it honest
var _ = customService

var fencedGoBlocks = regexp.MustCompile("(?s)```go\n(.*?)```")

// The fenced Go blocks in docs/sdk.md are real, compiled code: the first is
// main.go from the package clause down, the second is customService's body
// in options.go. The compiler catches API drift; this test catches wording
// drift between the documentation and the compiled examples.
func TestSDKDocExamplesMatchCompiledCode(t *testing.T) {
md, err := os.ReadFile("../../sdk.md")
assert.NilError(t, err)

blocks := fencedGoBlocks.FindAllStringSubmatch(string(md), -1)
assert.Equal(t, len(blocks), 2, "docs/sdk.md is expected to hold exactly two fenced Go blocks")

mainGo, err := os.ReadFile("main.go")
assert.NilError(t, err)
// the example file carries a license header and this explanatory comment
// above the package clause; the doc block starts at the package clause
_, code, found := strings.Cut(string(mainGo), "package main")
assert.Assert(t, found)
assert.Equal(t, blocks[0][1], "package main"+code,
"the first Go block of docs/sdk.md must match docs/examples/sdk/main.go from its package clause down")

optionsGo, err := os.ReadFile("options.go")
assert.NilError(t, err)
assert.Assert(t, strings.Contains(string(optionsGo), strings.TrimRight(blocks[1][1], "\n")),
"the second Go block of docs/sdk.md must appear verbatim in docs/examples/sdk/options.go")
}
123 changes: 64 additions & 59 deletions docs/sdk.md
Loading