@@ -20,6 +20,7 @@ import (
2020 "io/ioutil"
2121 "os"
2222 "path/filepath"
23+ "strings"
2324 "testing"
2425
2526 "github.com/pkg/errors"
@@ -77,7 +78,7 @@ devices:
7778 },
7879 } {
7980 t .Run (tc .name , func (t * testing.T ) {
80- file , err := mkTestFile (t , []byte (tc .data ))
81+ file , err := mkTestSpec (t , []byte (tc .data ))
8182 if err != nil {
8283 t .Errorf ("failed to create CDI Spec test file: %v" , err )
8384 return
@@ -461,7 +462,7 @@ devices:
461462 },
462463 } {
463464 t .Run (tc .name , func (t * testing.T ) {
464- file , err := mkTestFile (t , []byte (tc .data ))
465+ file , err := mkTestSpec (t , []byte (tc .data ))
465466 if err != nil {
466467 t .Errorf ("failed to create CDI Spec test file: %v" , err )
467468 return
@@ -493,8 +494,8 @@ devices:
493494}
494495
495496// Create an automatically cleaned up temporary file for a test.
496- func mkTestFile (t * testing.T , data []byte ) (string , error ) {
497- tmp , err := ioutil .TempFile ("" , ".cdi-test.*" )
497+ func mkTestSpec (t * testing.T , data []byte ) (string , error ) {
498+ tmp , err := ioutil .TempFile ("" , ".cdi-test.*." + specType ( data ) )
498499 if err != nil {
499500 return "" , errors .Wrapf (err , "failed to create test file" )
500501 }
@@ -515,6 +516,14 @@ func mkTestFile(t *testing.T, data []byte) (string, error) {
515516 return file , nil
516517}
517518
519+ func specType (content []byte ) string {
520+ spec := strings .TrimSpace (string (content ))
521+ if spec != "" && spec [0 ] == '{' {
522+ return "json"
523+ }
524+ return "yaml"
525+ }
526+
518527func TestCurrentVersionIsValid (t * testing.T ) {
519528 require .NoError (t , validateVersion (cdi .CurrentVersion ))
520529}
0 commit comments