-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Taking the PR from @kim0 and expanding on it. I wanted to be able to more easily test the formatters we have so far and make it easy to add more formatters later. [#7] Signed-off-by: Geoffrey Wiseman <[email protected]>
- Loading branch information
1 parent
8ce4091
commit 3a7cfc0
Showing
8 changed files
with
191 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package format | ||
|
||
import ( | ||
"fmt" | ||
"github.com/geoffreywiseman/gh-actions-usage/client" | ||
"os" | ||
) | ||
|
||
var formatters = map[string]Formatter{ | ||
"human": humanFormatter{os.Stdout}, | ||
"tsv": tsvFormatter{os.Stdout}, | ||
} | ||
|
||
// Formatter is an interface for formatting output from the extension, allowing the user to pick one of several output styles | ||
type Formatter interface { | ||
PrintUsage(usage client.RepoUsage) | ||
} | ||
|
||
// GetFormatter returns a formatter by name, or an error if the name is unknown | ||
func GetFormatter(name string) (Formatter, error) { | ||
f, ok := formatters[name] | ||
if !ok { | ||
return nil, fmt.Errorf("unknown formatter: %s", name) | ||
} | ||
return f, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package format | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestGetFormatters(t *testing.T) { | ||
type test struct { | ||
name string | ||
expectedType interface{} | ||
} | ||
tests := []test{ | ||
{name: "human", expectedType: humanFormatter{}}, | ||
{name: "tsv", expectedType: tsvFormatter{}}, | ||
{name: "yaml"}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
formatter, err := GetFormatter(tc.name) | ||
if tc.expectedType == nil { | ||
assert.Errorf(t, err, "unknown formatter %s", tc.name) | ||
} else { | ||
assert.IsType(t, tc.expectedType, formatter) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package format | ||
|
||
import ( | ||
"fmt" | ||
"github.com/geoffreywiseman/gh-actions-usage/client" | ||
"io" | ||
) | ||
|
||
type humanFormatter struct { | ||
w io.Writer | ||
} | ||
|
||
func (hf humanFormatter) PrintUsage(usage client.RepoUsage) { | ||
for repo, flowUsage := range usage { | ||
var lines = make([]string, 0, len(flowUsage)) | ||
var repoTotal uint | ||
for flow, usage := range flowUsage { | ||
repoTotal += usage | ||
line := fmt.Sprintf("- %s (%s, %s, %s)", flow.Name, flow.Path, flow.State, Humanize(usage)) | ||
lines = append(lines, line) | ||
} | ||
_, _ = fmt.Fprintf(hf.w, "%s (%d workflows; %s):\n", repo.FullName, len(usage[repo]), Humanize(repoTotal)) | ||
for _, line := range lines { | ||
_, _ = fmt.Fprintln(hf.w, line) | ||
} | ||
_, _ = fmt.Fprintln(hf.w) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package format | ||
|
||
import ( | ||
"bytes" | ||
"github.com/geoffreywiseman/gh-actions-usage/client" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestHumanFormatter(t *testing.T) { | ||
// Given | ||
var output bytes.Buffer | ||
formatter := humanFormatter{&output} | ||
|
||
wf := client.Workflow{Name: "CI", Path: ".github/workflows/ci.yml", State: "active"} | ||
wfu := make(client.WorkflowUsage) | ||
wfu[wf] = 50 | ||
r := client.Repository{FullName: "codiform/gh-actions-usage"} | ||
ru := make(client.RepoUsage) | ||
ru[&r] = wfu | ||
|
||
// When | ||
formatter.PrintUsage(ru) | ||
|
||
// Then | ||
assert.Equal(t, "codiform/gh-actions-usage (1 workflows; 50ms):\n- CI (.github/workflows/ci.yml, active, 50ms)\n\n", output.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package format | ||
|
||
import ( | ||
"fmt" | ||
"github.com/geoffreywiseman/gh-actions-usage/client" | ||
"io" | ||
) | ||
|
||
type tsvFormatter struct { | ||
w io.Writer | ||
} | ||
|
||
func (tf tsvFormatter) PrintUsage(usage client.RepoUsage) { | ||
_, _ = fmt.Fprintf(tf.w, "%s\t%s\t%s\n", "Repo", "Workflow", "Milliseconds") | ||
for repo, flowUsage := range usage { | ||
for workflow, usage := range flowUsage { | ||
_, _ = fmt.Fprintf(tf.w, "%s\t%s\t%d\n", repo.FullName, workflow.Path, usage) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package format | ||
|
||
import ( | ||
"bytes" | ||
"github.com/geoffreywiseman/gh-actions-usage/client" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestTsvFormatter(t *testing.T) { | ||
// Given | ||
var output bytes.Buffer | ||
formatter := tsvFormatter{&output} | ||
|
||
wf := client.Workflow{Name: "Security", Path: ".github/workflows/DevSecOps.yaml", State: "alert"} | ||
wfu := make(client.WorkflowUsage) | ||
wfu[wf] = 2500 | ||
r := client.Repository{FullName: "codiform/gh-actions-usage"} | ||
ru := make(client.RepoUsage) | ||
ru[&r] = wfu | ||
|
||
// When | ||
formatter.PrintUsage(ru) | ||
|
||
// Then | ||
assert.Equal(t, "Repo\tWorkflow\tMilliseconds\ncodiform/gh-actions-usage\t.github/workflows/DevSecOps.yaml\t2500\n", output.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters