Skip to content

Commit

Permalink
fix 🐛: simplified manpage for smaller binary size
Browse files Browse the repository at this point in the history
Signed-off-by: moualhi zine el abidine <zmoualhi@outlook.com>
  • Loading branch information
muandane committed Aug 27, 2024
1 parent 040b444 commit 39f8174
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions cmd/man_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cmd

import (
"bytes"
"io"
"os"
"strings"
"testing"

"github.com/spf13/cobra"
)

// captureOutput captures and returns the output of a function that prints to stdout.
func captureOutput(f func()) string {
// Save the current stdout
oldStdout := os.Stdout

// Create a pipe to capture stdout
r, w, _ := os.Pipe()
os.Stdout = w

// Execute the function
f()

// Close the writer and restore stdout
_ = w.Close()
os.Stdout = oldStdout

// Read the captured output
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
_ = r.Close()

return buf.String()
}

// TestManCmd tests the `manCmd` to ensure it outputs the manual correctly.
func TestManCmd(t *testing.T) {
// Capturing the output of the manCmd.
output := captureOutput(func() {
manCmd.Run(&cobra.Command{}, []string{})
})

// Check that the output contains key sections.
sections := []string{
"NAME",
"SYNOPSIS",
"DESCRIPTION",
"OPTIONS",
"EXAMPLES",
"AUTHOR",
"COPYRIGHT",
}

for _, section := range sections {
if !strings.Contains(output, section) {
t.Errorf("Expected section %s in man page output, but it was not found.", section)
}
}
}

0 comments on commit 39f8174

Please sign in to comment.