|
| 1 | +package project |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "net/url" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + openerrors "github.com/opf/openproject-cli/components/errors" |
| 12 | + "github.com/opf/openproject-cli/components/printer" |
| 13 | + "github.com/opf/openproject-cli/components/requests" |
| 14 | + "github.com/opf/openproject-cli/components/routes" |
| 15 | +) |
| 16 | + |
| 17 | +func TestInspectBrowserFailureReturnsError(t *testing.T) { |
| 18 | + server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { |
| 19 | + if request.Method != http.MethodGet { |
| 20 | + t.Errorf("request method = %s, want GET", request.Method) |
| 21 | + } |
| 22 | + response.Header().Set("Content-Type", "application/json") |
| 23 | + _, _ = response.Write([]byte(`{"id":1,"identifier":"example","name":"Example"}`)) |
| 24 | + })) |
| 25 | + t.Cleanup(server.Close) |
| 26 | + |
| 27 | + host, err := url.Parse(server.URL) |
| 28 | + if err != nil { |
| 29 | + t.Fatal(err) |
| 30 | + } |
| 31 | + requests.Init(host, "", false) |
| 32 | + routes.Init(host) |
| 33 | + |
| 34 | + testingPrinter := &printer.TestingPrinter{} |
| 35 | + printer.Init(testingPrinter) |
| 36 | + t.Setenv("PATH", t.TempDir()) |
| 37 | + openInBrowser = true |
| 38 | + t.Cleanup(func() { |
| 39 | + openInBrowser = false |
| 40 | + }) |
| 41 | + |
| 42 | + err = inspectProject(nil, []string{"1"}) |
| 43 | + if !errors.Is(err, openerrors.ErrHandled) { |
| 44 | + t.Fatalf("inspectProject error = %v, want ErrHandled", err) |
| 45 | + } |
| 46 | + if count := strings.Count(testingPrinter.ErrResult, "[ERROR]"); count != 1 { |
| 47 | + t.Errorf("error diagnostic count = %d, want 1; stderr: %q", count, testingPrinter.ErrResult) |
| 48 | + } |
| 49 | +} |
0 commit comments