Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xcode Test Results as Bitrise environment variables #245

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions output/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package output

import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/bitrise-io/bitrise/configs"
Expand Down Expand Up @@ -29,6 +32,29 @@ type exporter struct {
testAddonExporter testaddon.Exporter
}

type TestSummary struct {
Actions []struct {
ActionResult struct {
TestsRef struct {
Id struct {
Value string `json:"_value"`
} `json:"id"`
} `json:"testsRef"`
} `json:"actionResult"`
} `json:"actions"`
}

type TestResults struct {
Summary struct {
TestsPassedCount struct {
Value int `json:"_value"`
} `json:"testsPassedCount"`
TestsFailedCount struct {
Value int `json:"_value"`
} `json:"testsFailedCount"`
} `json:"summary"`
}

// NewExporter ...
func NewExporter(envRepository env.Repository, logger log.Logger, outputExporter export.Exporter, testAddonExporter testaddon.Exporter) Exporter {
return &exporter{
Expand Down Expand Up @@ -73,6 +99,11 @@ func (e exporter) ExportXCResultBundle(deployDir, xcResultPath, scheme string) {
e.logger.Warnf("Failed to export test results: %s", err)
}
}

// Parse the test results and set the environment variables
if err := e.ExportTestResults(xcResultPath); err != nil {
e.logger.Warnf("Failed to export test results: %s", err)
}
}

func (e exporter) ExportXcodebuildBuildLog(deployDir, xcodebuildBuildLog string) error {
Expand Down Expand Up @@ -119,3 +150,52 @@ func (e exporter) ExportSimulatorDiagnostics(deployDir, pth, name string) error

return nil
}

func (e exporter) ExportTestResults(xcResultPath string) error {
// Convert xcresult to JSON
cmd := exec.Command("xcrun", "xcresulttool", "get", "--path", xcResultPath, "--format", "json")
output, err := cmd.Output()
if err != nil {
return fmt.Errorf("error running xcresulttool: %w", err)
}

// Parse JSON output to get the test summary ID
var summary TestSummary
err = json.Unmarshal(output, &summary)
if err != nil {
return fmt.Errorf("error parsing JSON: %w", err)
}

testSummaryId := summary.Actions[0].ActionResult.TestsRef.Id.Value

// Get test results using the test summary ID
cmd = exec.Command("xcrun", "xcresulttool", "get", "--path", xcResultPath, "--id", testSummaryId, "--format", "json")
output, err = cmd.Output()
if err != nil {
return fmt.Errorf("error running xcresulttool for test results: %w", err)
}

// Parse JSON output to get the test results
var results TestResults
err = json.Unmarshal(output, &results)
if err != nil {
return fmt.Errorf("error parsing JSON: %w", err)
}

// Set environment variables for the number of tests passed and failed
passedCount := results.Summary.TestsPassedCount.Value
failedCount := results.Summary.TestsFailedCount.Value

if err := e.envRepository.Set("BITRISE_XCODE_TESTS_PASSED_COUNT", fmt.Sprintf("%d", passedCount)); err != nil {
e.logger.Warnf("Failed to export: BITRISE_XCODE_TESTS_PASSED_COUNT: %s", err)
}

if err := e.envRepository.Set("BITRISE_XCODE_TESTS_FAILED_COUNT", fmt.Sprintf("%d", failedCount)); err != nil {
e.logger.Warnf("Failed to export: BITRISE_XCODE_TESTS_FAILED_COUNT: %s", err)
}

e.logger.Printf("Number of tests passed: %d", passedCount)
e.logger.Printf("Number of tests failed: %d", failedCount)

return nil
}