Skip to content

Commit

Permalink
Add test for printer method
Browse files Browse the repository at this point in the history
Signed-off-by: jesus m. rodriguez <[email protected]>
  • Loading branch information
jmrodri committed Sep 30, 2022
1 parent adfae2b commit d32b291
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions internal/gh/printer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright © 2022 jesus m. rodriguez [email protected]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gh

import (
"fmt"
"io"
"os"

"github.com/google/go-github/v47/github"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var expectedLong = `Issue: 123
State: open
Issue 1
`
var _ = Describe("Printer", func() {

Describe("PrintGithubIssue", func() {
var (
issue *github.Issue
r, w, tmp *os.File
)
BeforeEach(func() {
issue = &github.Issue{
ID: github.Int64(12213123),
Number: github.Int(123),
Title: github.String("Issue 1"),
State: github.String("open"),
Body: github.String("body of the issue"),
URL: github.String("https://api.github.com/repos/foo/bar/issues/123"),
}

r, w, _ = os.Pipe()
tmp = os.Stdout
os.Stdout = w
})
AfterEach(func() {
os.Stdout = tmp
})
It("should print issue in color & one line", func() {
go func() {
PrintGithubIssue(issue, true, true)
w.Close()
}()

stdout, _ := io.ReadAll(r)

expected := fmt.Sprintf("\033[33m%5d\033[0m \033[32m%s\033[0m %s\n",
issue.GetNumber(), issue.GetState(), issue.GetTitle())

Expect(expected).To(Equal(string(stdout)))
})
It("should ignore color flag if not printing in oneline", func() {
go func() {
PrintGithubIssue(issue, false, true)
w.Close()
}()

stdout, _ := io.ReadAll(r)

Expect(expectedLong).To(Equal(string(stdout)))
})
It("should print in oneline but not in color", func() {
go func() {
PrintGithubIssue(issue, true, false)
w.Close()
}()

stdout, _ := io.ReadAll(r)

expected := fmt.Sprintf("%5d %s %s\n", issue.GetNumber(),
issue.GetState(), issue.GetTitle())

Expect(expected).To(Equal(string(stdout)))
})
It("should print full output not in color", func() {
go func() {
PrintGithubIssue(issue, false, false)
w.Close()
}()

stdout, _ := io.ReadAll(r)

Expect(expectedLong).To(Equal(string(stdout)))
})
})
})

0 comments on commit d32b291

Please sign in to comment.