File tree Expand file tree Collapse file tree 3 files changed +52
-2
lines changed
Expand file tree Collapse file tree 3 files changed +52
-2
lines changed Original file line number Diff line number Diff line change 1+ // Copyright 2026 Google LLC
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // https://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
14+
15+ package yaml
16+
17+ const copyright = `# Copyright 2026 Google LLC
18+ #
19+ # Licensed under the Apache License, Version 2.0 (the "License");
20+ # you may not use this file except in compliance with the License.
21+ # You may obtain a copy of the License at
22+ #
23+ # https://www.apache.org/licenses/LICENSE-2.0
24+ #
25+ # Unless required by applicable law or agreed to in writing, software
26+ # distributed under the License is distributed on an "AS IS" BASIS,
27+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+ # See the License for the specific language governing permissions and
29+ # limitations under the License.
30+
31+ `
Original file line number Diff line number Diff line change @@ -72,13 +72,14 @@ func Read[T any](path string) (*T, error) {
7272 return Unmarshal [T ](data )
7373}
7474
75- // Write marshals a value to YAML, formats it with yamlfmt, and writes it to a
76- // file.
75+ // Write marshals a value to YAML, formats it with yamlfmt, adds a copyright header
76+ // and writes it to a file.
7777func Write (path string , v any ) error {
7878 data , err := Marshal (v )
7979 if err != nil {
8080 return err
8181 }
82+ data = append ([]byte (copyright ), data ... )
8283 return os .WriteFile (path , data , 0644 )
8384}
8485
Original file line number Diff line number Diff line change 1515package yaml
1616
1717import (
18+ "os"
1819 "path/filepath"
1920 "testing"
2021
@@ -74,6 +75,23 @@ func TestReadWrite(t *testing.T) {
7475 }
7576}
7677
78+ func TestWrite (t * testing.T ) {
79+ want := copyright + `name: test
80+ version: v1.0.0
81+ `
82+ path := filepath .Join (t .TempDir (), "test.yaml" )
83+ if err := Write (path , & testConfig {Name : "test" , Version : "v1.0.0" }); err != nil {
84+ t .Fatal (err )
85+ }
86+ got , err := os .ReadFile (path )
87+ if err != nil {
88+ t .Fatal (err )
89+ }
90+ if diff := cmp .Diff (want , string (got )); diff != "" {
91+ t .Errorf ("mismatch (-want +got):\n %s" , diff )
92+ }
93+ }
94+
7795func TestReadError (t * testing.T ) {
7896 _ , err := Read [testConfig ]("/nonexistent/path/file.yaml" )
7997 if err == nil {
You can’t perform that action at this time.
0 commit comments