|
| 1 | +// Copyright © 2022 jesus m. rodriguez [email protected] |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package gh |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "io" |
| 20 | + "os" |
| 21 | + |
| 22 | + "github.com/google/go-github/v47/github" |
| 23 | + |
| 24 | + . "github.com/onsi/ginkgo" |
| 25 | + . "github.com/onsi/gomega" |
| 26 | +) |
| 27 | + |
| 28 | +var expectedLong = `Issue: 123 |
| 29 | +State: open |
| 30 | +
|
| 31 | + Issue 1 |
| 32 | +
|
| 33 | +` |
| 34 | +var _ = Describe("Printer", func() { |
| 35 | + |
| 36 | + Describe("PrintGithubIssue", func() { |
| 37 | + var ( |
| 38 | + issue *github.Issue |
| 39 | + r, w, tmp *os.File |
| 40 | + ) |
| 41 | + BeforeEach(func() { |
| 42 | + issue = &github.Issue{ |
| 43 | + ID: github.Int64(12213123), |
| 44 | + Number: github.Int(123), |
| 45 | + Title: github.String("Issue 1"), |
| 46 | + State: github.String("open"), |
| 47 | + Body: github.String("body of the issue"), |
| 48 | + URL: github.String("https://api.github.com/repos/foo/bar/issues/123"), |
| 49 | + } |
| 50 | + |
| 51 | + r, w, _ = os.Pipe() |
| 52 | + tmp = os.Stdout |
| 53 | + os.Stdout = w |
| 54 | + }) |
| 55 | + AfterEach(func() { |
| 56 | + os.Stdout = tmp |
| 57 | + }) |
| 58 | + It("should print issue in color & one line", func() { |
| 59 | + go func() { |
| 60 | + PrintGithubIssue(issue, true, true) |
| 61 | + w.Close() |
| 62 | + }() |
| 63 | + |
| 64 | + stdout, _ := io.ReadAll(r) |
| 65 | + |
| 66 | + expected := fmt.Sprintf("\033[33m%5d\033[0m \033[32m%s\033[0m %s\n", |
| 67 | + issue.GetNumber(), issue.GetState(), issue.GetTitle()) |
| 68 | + |
| 69 | + Expect(expected).To(Equal(string(stdout))) |
| 70 | + }) |
| 71 | + It("should ignore color flag if not printing in oneline", func() { |
| 72 | + go func() { |
| 73 | + PrintGithubIssue(issue, false, true) |
| 74 | + w.Close() |
| 75 | + }() |
| 76 | + |
| 77 | + stdout, _ := io.ReadAll(r) |
| 78 | + |
| 79 | + Expect(expectedLong).To(Equal(string(stdout))) |
| 80 | + }) |
| 81 | + It("should print in oneline but not in color", func() { |
| 82 | + go func() { |
| 83 | + PrintGithubIssue(issue, true, false) |
| 84 | + w.Close() |
| 85 | + }() |
| 86 | + |
| 87 | + stdout, _ := io.ReadAll(r) |
| 88 | + |
| 89 | + expected := fmt.Sprintf("%5d %s %s\n", issue.GetNumber(), |
| 90 | + issue.GetState(), issue.GetTitle()) |
| 91 | + |
| 92 | + Expect(expected).To(Equal(string(stdout))) |
| 93 | + }) |
| 94 | + It("should print full output not in color", func() { |
| 95 | + go func() { |
| 96 | + PrintGithubIssue(issue, false, false) |
| 97 | + w.Close() |
| 98 | + }() |
| 99 | + |
| 100 | + stdout, _ := io.ReadAll(r) |
| 101 | + |
| 102 | + Expect(expectedLong).To(Equal(string(stdout))) |
| 103 | + }) |
| 104 | + }) |
| 105 | +}) |
0 commit comments