github.com/grafviktor/keep-my-secret@v0.9.10-0.20230908165355-19f35cce90e5/internal/version/version_test.go (about) 1 package version 2 3 import ( 4 "bytes" 5 "io" 6 "os" 7 "testing" 8 ) 9 10 func TestSetAndGet(t *testing.T) { 11 // Test Set function to update build information 12 Set("1.0", "2023-09-01", "abcdef") 13 14 // Check if the values are correctly updated 15 if BuildVersion() != "1.0" { 16 t.Errorf("Expected BuildVersion() to return '1.0', but got '%s'", BuildVersion()) 17 } 18 19 if BuildDate() != "2023-09-01" { 20 t.Errorf("Expected BuildDate() to return '2023-09-01', but got '%s'", BuildDate()) 21 } 22 23 if BuildCommit() != "abcdef" { 24 t.Errorf("Expected BuildCommit() to return 'abcdef', but got '%s'", BuildCommit()) 25 } 26 } 27 28 func TestPrintConsole(t *testing.T) { 29 // Capture the output of PrintConsole 30 output := captureOutput(func() { 31 PrintConsole() 32 }) 33 34 // Verify that the printed output matches the expected format 35 expectedOutput := "Build version: 1.0\nBuild date: 2023-09-01\nBuild commit: abcdef\n" 36 37 if output != expectedOutput { 38 t.Errorf("Printed output does not match the expected output.\nExpected:\n%s\nActual:\n%s", expectedOutput, output) 39 } 40 } 41 42 // captureOutput captures the output of a function and returns it as a string 43 func captureOutput(f func()) string { 44 oldStdout := os.Stdout 45 r, w, _ := os.Pipe() 46 os.Stdout = w 47 48 f() 49 50 w.Close() 51 os.Stdout = oldStdout 52 53 var buf bytes.Buffer 54 _, _ = io.Copy(&buf, r) 55 56 return buf.String() 57 }