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
11 changes: 11 additions & 0 deletions eng/_util/cmd/updatecryptodocs/updatecryptodocs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"path/filepath"
"strings"
"testing"

"github.com/microsoft/go/_util/internal/docutil"
)

func TestUpdateCryptoDocsReproducible(t *testing.T) {
Expand All @@ -17,6 +19,7 @@ func TestUpdateCryptoDocsReproducible(t *testing.T) {
if err != nil {
t.Fatalf("Failed to read checked-in document at %q: %v", checkedInPath, err)
}
skipIfCopiedDocument(t, checkedIn)

generated, err := generate()
if err != nil {
Expand All @@ -37,6 +40,7 @@ func TestUpdateUserGuideReproducible(t *testing.T) {
if err != nil {
t.Fatalf("Failed to read checked-in document at %q: %v", checkedInPath, err)
}
skipIfCopiedDocument(t, checkedIn)

generated, err := generateUserGuide()
if err != nil {
Expand All @@ -50,3 +54,10 @@ func TestUpdateUserGuideReproducible(t *testing.T) {
t.Errorf("Generated document does not match checked-in document. Run this command to update:\n pwsh eng/run.ps1 updatecryptodocs")
}
}

func skipIfCopiedDocument(t *testing.T, contents []byte) {
t.Helper()
if docutil.IsCopied(contents) {
t.Skip("Document is maintained by automatic copying, not local regeneration.")
}
}
13 changes: 13 additions & 0 deletions eng/_util/internal/docutil/docutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package docutil

import "strings"

// IsCopied reports whether contents start with the automatic documentation copy notice.
func IsCopied(contents []byte) bool {
normalized := strings.ReplaceAll(string(contents), "\r\n", "\n")
return strings.HasPrefix(normalized, "<!--\nDO NOT EDIT THIS COPY.\nLocal edits will be overwritten.")
}
34 changes: 34 additions & 0 deletions eng/_util/internal/docutil/docutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package docutil

import (
"strings"
"testing"
)

func TestIsCopied(t *testing.T) {
const prefix = "<!--\nDO NOT EDIT THIS COPY.\nLocal edits will be overwritten."
for _, tt := range []struct {
name string
contents string
want bool
}{
{"copy", prefix + "\n-->\nCopied document.\n", true},
{"copy_crlf", strings.ReplaceAll(prefix+"\n-->\nCopied document.\n", "\n", "\r\n"), true},
{"prefix_only", prefix, true},
{"empty", "", false},
{"local", "Generated document.\n", false},
{"partial_prefix", "<!--\nDO NOT EDIT THIS COPY.\n", false},
{"prefix_in_body", "Generated document.\n" + prefix, false},
{"leading_newline", "\n" + prefix, false},
} {
t.Run(tt.name, func(t *testing.T) {
if got := IsCopied([]byte(tt.contents)); got != tt.want {
t.Errorf("IsCopied() = %v, want %v", got, tt.want)
}
})
}
}
Loading