From 7a2bd7aaf6a27bfb68558252fd6c513a504b17df Mon Sep 17 00:00:00 2001 From: Benjamin Cane Date: Sun, 29 Jan 2023 16:13:53 +0000 Subject: [PATCH] Fixing code examples --- README.md | 45 ++++++++++++++++----------------------------- testcerts.go | 20 ++++++-------------- 2 files changed, 22 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index afbcc68..94ddf34 100644 --- a/README.md +++ b/README.md @@ -17,17 +17,14 @@ Overall, testcerts simplifies the process of generating and managing test certif The `GenerateCertsToFile` function generates an X.509 certificate and key and writes them to the file paths provided. ```go -package main - -import ( - "github.com/madflojo/testcerts" -) - -func main() { - err := testcerts.GenerateCertsToFile("/tmp/cert.pem", "/tmp/key.pem") +func TestSomething(t *testing.T) { + err := testcerts.GenerateCertsToFile("/tmp/cert", "/tmp/key") if err != nil { - // handle error + // do stuff } + + _ = something.Run("/tmp/cert", "/tmp/key") + // do more testing } ``` @@ -36,17 +33,14 @@ func main() { The `GenerateCertsToTempFile` function generates an X.509 certificate and key and writes them to randomly generated files in the directory provided or the system's temporary directory if none is provided. The function returns the file paths of the generated files. ```go -package main - -import ( - "github.com/madflojo/testcerts" -) - -func main() { +func TestSomething(t *testing.T) { certFile, keyFile, err := testcerts.GenerateCertsToTempFile("/tmp/") if err != nil { - // handle error + // do stuff } + + _ = something.Run(certFile, keyFile) + // do more testing } ``` @@ -55,21 +49,14 @@ func main() { The `GenerateCerts` function generates an X.509 certificate and key and returns them as byte slices. ```go -package main - -import ( - "fmt" - - "github.com/madflojo/testcerts" -) - -func main() { +func TestSomething(t *testing.T) { cert, key, err := testcerts.GenerateCerts() if err != nil { - // handle error + // do stuff } - fmt.Printf("Certificate: %s\n", cert) - fmt.Printf("Key: %s\n", key) + + _ = something.Run(cert, key) + // do more testing } ``` diff --git a/testcerts.go b/testcerts.go index acb596d..57ca702 100644 --- a/testcerts.go +++ b/testcerts.go @@ -7,22 +7,14 @@ named file in a specified or temporary directory. For example, to generate and save a certificate and key to a temporary directory: - package main - - import ( - "fmt" - "log" - - "testcerts" - ) - - func main() { - certPath, keyPath, err := testcerts.GenerateCertsToTempFile("") + func TestSomething(t *testing.T) { + certFile, keyFile, err := testcerts.GenerateCertsToTempFile("/tmp/") if err != nil { - log.Fatal(err) + // do stuff } - fmt.Println("Certificate written to:", certPath) - fmt.Println("Key written to:", keyPath) + + _ = something.Run(certFile, keyFile) + // do more testing } This will create a temporary certificate and key and print the paths to where the files were written.