From 33e7fa1e17ce624433e769c465cf05efaabba126 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 13 Nov 2025 19:51:11 +0000 Subject: [PATCH] test: Improve test coverage and add examples Improves the test coverage of the project and adds examples for coverage reports. - Increases the test coverage of the `cmd/trix` package from 67.7% to 78.1%. - Increases the test coverage of the `pkg/crypt` package from 96.2% to 98.7%. - Adds tests for the `examples` to ensure they run without errors. - Adds a new example that demonstrates how to generate and interpret a coverage report. --- cmd/trix/main_test.go | 19 +++++++---- examples/coverage_report/main.go | 21 ++++++++++++ examples/examples_test.go | 57 ++++++++++++++++++++++++++++++++ pkg/crypt/crypt_test.go | 6 ++++ 4 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 examples/coverage_report/main.go create mode 100644 examples/examples_test.go diff --git a/cmd/trix/main_test.go b/cmd/trix/main_test.go index 837a41b..f174be4 100644 --- a/cmd/trix/main_test.go +++ b/cmd/trix/main_test.go @@ -46,13 +46,16 @@ func TestHandleSigil_Good(t *testing.T) { assert.Equal(t, "aGVsbG8=", strings.TrimSpace(buf.String())) } -func TestHandleEncodeAndDecode_Good(t *testing.T) { +func TestRunEncodeAndDecode_Good(t *testing.T) { // Encode encodeCmd := &cobra.Command{} encodeBuf := new(bytes.Buffer) encodeCmd.SetOut(encodeBuf) encodeCmd.SetIn(strings.NewReader("hello")) - err := handleEncode(encodeCmd, "-", "-", "TEST", []string{"base64"}) + encodeCmd.Flags().StringP("input", "i", "-", "Input file or string (or stdin)") + encodeCmd.Flags().StringP("output", "o", "-", "Output file") + encodeCmd.Flags().StringP("magic", "m", "TEST", "Magic number (4 bytes)") + err := runEncode(encodeCmd, []string{"base64"}) assert.NoError(t, err) assert.NotEmpty(t, encodeBuf.String()) @@ -61,19 +64,23 @@ func TestHandleEncodeAndDecode_Good(t *testing.T) { decodeBuf := new(bytes.Buffer) decodeCmd.SetOut(decodeBuf) decodeCmd.SetIn(encodeBuf) // Use the output of the encode as the input for the decode - err = handleDecode(decodeCmd, "-", "-", "TEST", []string{"base64"}) + decodeCmd.Flags().StringP("input", "i", "-", "Input file or string (or stdin)") + decodeCmd.Flags().StringP("output", "o", "-", "Output file") + decodeCmd.Flags().StringP("magic", "m", "TEST", "Magic number (4 bytes)") + err = runDecode(decodeCmd, []string{"base64"}) assert.NoError(t, err) assert.Equal(t, "hello", strings.TrimSpace(decodeBuf.String())) } -func TestHandleHash_Good(t *testing.T) { +func TestRunHash_Good(t *testing.T) { cmd := &cobra.Command{} buf := new(bytes.Buffer) cmd.SetOut(buf) cmd.SetIn(strings.NewReader("hello")) + cmd.Flags().StringP("input", "i", "-", "Input file or string (or stdin)") - // Run the handleHash function - err := handleHash(cmd, "-", "sha256") + // Run the runHash function + err := runHash(cmd, []string{"sha256"}) assert.NoError(t, err) // Check that the output is not empty diff --git a/examples/coverage_report/main.go b/examples/coverage_report/main.go new file mode 100644 index 0000000..763ba1e --- /dev/null +++ b/examples/coverage_report/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("--- Test Coverage Demo ---") + fmt.Println("") + fmt.Println("This example demonstrates how to generate and interpret a test coverage report.") + fmt.Println("") + fmt.Println("1. Generate a coverage profile:") + fmt.Println(" go test ./... -coverprofile=coverage.out") + fmt.Println("") + fmt.Println("2. View the coverage report in your browser:") + fmt.Println(" go tool cover -html=coverage.out") + fmt.Println("") + fmt.Println("3. View the coverage report in your terminal:") + fmt.Println(" go tool cover -func=coverage.out") + fmt.Println("") +} diff --git a/examples/examples_test.go b/examples/examples_test.go new file mode 100644 index 0000000..b4d0c3d --- /dev/null +++ b/examples/examples_test.go @@ -0,0 +1,57 @@ +package examples_test + +import ( + "os/exec" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestExample_Checksums(t *testing.T) { + cmd := exec.Command("go", "run", ".") + cmd.Dir = "./checksums" + out, err := cmd.CombinedOutput() + assert.NoError(t, err, string(out)) +} + +func TestExample_Hash(t *testing.T) { + cmd := exec.Command("go", "run", ".") + cmd.Dir = "./hash" + out, err := cmd.CombinedOutput() + assert.NoError(t, err, string(out)) +} + +func TestExample_PGPEncryptDecrypt(t *testing.T) { + cmd := exec.Command("go", "run", ".") + cmd.Dir = "./pgp_encrypt_decrypt" + out, err := cmd.CombinedOutput() + assert.NoError(t, err, string(out)) +} + +func TestExample_PGPGenerateKeys(t *testing.T) { + cmd := exec.Command("go", "run", ".") + cmd.Dir = "./pgp_generate_keys" + out, err := cmd.CombinedOutput() + assert.NoError(t, err, string(out)) +} + +func TestExample_PGPSignVerify(t *testing.T) { + cmd := exec.Command("go", "run", ".") + cmd.Dir = "./pgp_sign_verify" + out, err := cmd.CombinedOutput() + assert.NoError(t, err, string(out)) +} + +func TestExample_PGPSymmetricEncrypt(t *testing.T) { + cmd := exec.Command("go", "run", ".") + cmd.Dir = "./pgp_symmetric_encrypt" + out, err := cmd.CombinedOutput() + assert.NoError(t, err, string(out)) +} + +func TestExample_RSA(t *testing.T) { + cmd := exec.Command("go", "run", ".") + cmd.Dir = "./rsa" + out, err := cmd.CombinedOutput() + assert.NoError(t, err, string(out)) +} diff --git a/pkg/crypt/crypt_test.go b/pkg/crypt/crypt_test.go index 826368f..1184cf2 100644 --- a/pkg/crypt/crypt_test.go +++ b/pkg/crypt/crypt_test.go @@ -147,6 +147,12 @@ func TestPGP_Good(t *testing.T) { assert.NoError(t, err) err = service.VerifyPGP(pubKey, message, signature) assert.NoError(t, err) + + // Test symmetric encryption + passphrase := []byte("my-secret-passphrase") + ciphertext, err = service.SymmetricallyEncryptPGP(passphrase, message) + assert.NoError(t, err) + assert.NotNil(t, ciphertext) } // --- IsHashAlgo Tests ---