blob: 0f28602c7f2de7a79b8bf71248cf87a9b6b75690 [file]
// Copyright 2026 Google LLC
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package copyright_headers
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.skia.org/infra/go/exec"
"go.skia.org/infra/go/git/git_common"
"go.skia.org/infra/go/testutils"
vfs_mocks "go.skia.org/infra/go/vfs/mocks"
)
func TestIsMissingCopyrightHeader(t *testing.T) {
assert.False(t, isMissingCopyrightHeader(`// Copyright 2026 Google LLC
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package foo
`))
assert.False(t, isMissingCopyrightHeader(`// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package foo
`))
assert.False(t, isMissingCopyrightHeader(`// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
package mocks
`))
assert.False(t, isMissingCopyrightHeader(`package spanner
// Generated by //go/sql/exporter/
// DO NOT EDIT
const Schema = ""
`))
assert.True(t, isMissingCopyrightHeader(`// Package foo does things.
package foo
`))
assert.True(t, isMissingCopyrightHeader(`package main
const tmpl = "// Code generated by foo; DO NOT EDIT\npackage bar\n"
`))
}
func TestAddCopyrightHeadersToGoFiles(t *testing.T) {
fs := vfs_mocks.NewFS(t)
vfs_mocks.SetupMockFS(t, fs, map[string]string{
"needs_header.go": `// Package a is a test package.
package a
`,
"already_has_header.go": `// Copyright 2020 Google LLC
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package a
`,
"generated.go": `// Code generated by foo. DO NOT EDIT.
package a
`,
})
ctx := git_common.WithGitFinder(context.Background(), func() (string, error) {
return "/bin/git", nil
})
ctx = exec.NewContext(ctx, func(ctx context.Context, cmd *exec.Command) error {
if len(cmd.Args) > 0 && cmd.Args[0] == "--version" {
_, err := cmd.CombinedOutput.Write([]byte("git version 2.40.0\n"))
return err
}
if len(cmd.Args) > 0 && cmd.Args[0] == "ls-files" {
assert.Equal(t, []string{"ls-files", "--", "needs_header.go", "already_has_header.go", "generated.go"}, cmd.Args)
_, err := cmd.CombinedOutput.Write([]byte("needs_header.go\nalready_has_header.go\ngenerated.go\n"))
return err
}
assert.Equal(t, []string{"log", "--follow", "--format=%ad", "--date=format:%Y", "--", "needs_header.go"}, cmd.Args)
_, err := cmd.CombinedOutput.Write([]byte("2024\n2021\n"))
return err
})
// Dry run should report needs_header.go without modifying it (no Create call on fs).
updated, err := addCopyrightHeadersToGoFiles(ctx, fs, "/repo", []string{"needs_header.go", "already_has_header.go", "generated.go"}, true)
require.NoError(t, err)
assert.Equal(t, []string{"needs_header.go"}, updated)
// Non-dry run should prepend the 2021 header to needs_header.go.
expectedContent := []byte(`// Copyright 2021 Google LLC
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Package a is a test package.
package a
`)
writtenFile := vfs_mocks.NewFile(t)
writtenFile.On("Write", testutils.AnyContext, expectedContent).Return(len(expectedContent), nil).Once()
writtenFile.On("Close", testutils.AnyContext).Return(nil).Once()
fs.On("Create", testutils.AnyContext, "needs_header.go").Return(writtenFile, nil).Once()
updated, err = addCopyrightHeadersToGoFiles(ctx, fs, "/repo", []string{"needs_header.go", "already_has_header.go", "generated.go"}, false)
require.NoError(t, err)
assert.Equal(t, []string{"needs_header.go"}, updated)
}
func TestFindGoFiles(t *testing.T) {
ctx := git_common.WithGitFinder(context.Background(), func() (string, error) {
return "/bin/git", nil
})
ctx = exec.NewContext(ctx, func(ctx context.Context, cmd *exec.Command) error {
if len(cmd.Args) > 0 && cmd.Args[0] == "--version" {
_, err := cmd.CombinedOutput.Write([]byte("git version 2.40.0\n"))
return err
}
assert.Equal(t, []string{"ls-files", "--", "a"}, cmd.Args)
_, err := cmd.CombinedOutput.Write([]byte("a/b.go\na/README.md\na/a.go\n"))
return err
})
files, err := findGoFiles(ctx, "/repo", []string{"a"})
require.NoError(t, err)
assert.Equal(t, []string{"a/a.go", "a/b.go"}, files)
}