-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal: add some helper functions to support CVE v5
Adds various helper functions that will be used to add support for the new CVE v5 schema. For golang/go#49289 Change-Id: I3e9aaa95e30000c01a3f6b5738950b9dccdd84cc Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/545296 Reviewed-by: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
- Loading branch information
Showing
6 changed files
with
119 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2023 The Go 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 test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"golang.org/x/tools/txtar" | ||
) | ||
|
||
func WriteTxtar(filename string, files []txtar.File, comment string) error { | ||
if err := os.MkdirAll(filepath.Dir(filename), os.ModePerm); err != nil { | ||
return err | ||
} | ||
|
||
if err := os.WriteFile(filename, txtar.Format( | ||
&txtar.Archive{ | ||
Comment: []byte(addCopyright(comment)), | ||
Files: files, | ||
}, | ||
), os.ModePerm); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func addCopyright(comment string) string { | ||
return fmt.Sprintf("%s\n\n%s\n\n", copyright, comment) | ||
} | ||
|
||
var copyright = fmt.Sprintf(`Copyright %d The Go Authors. All rights reserved. | ||
Use of this source code is governed by a BSD-style | ||
license that can be found in the LICENSE file.`, currentYear()) | ||
|
||
func currentYear() int { | ||
year, _, _ := time.Now().Date() | ||
return year | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2023 The Go 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 test | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"golang.org/x/tools/txtar" | ||
) | ||
|
||
func TestWriteTxtar(t *testing.T) { | ||
tmp := t.TempDir() | ||
|
||
filename := filepath.Join(tmp, "example", "file.txtar") | ||
files := []txtar.File{ | ||
{ | ||
Name: "a.txt", | ||
Data: []byte("abcdefg\n"), | ||
}, | ||
{ | ||
Name: "b.txt", | ||
Data: []byte("hijklmnop\n"), | ||
}, | ||
} | ||
comment := "Context about this archive" | ||
|
||
if err := WriteTxtar(filename, files, comment); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
got, err := txtar.ParseFile(filename) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
want := &txtar.Archive{ | ||
Comment: []byte(addCopyright(comment)), | ||
Files: files, | ||
} | ||
|
||
if diff := cmp.Diff(want, got); diff != "" { | ||
t.Errorf("mismatch (-want, +got):\n%s", diff) | ||
} | ||
} |