Skip to content

Commit

Permalink
Add Support for Parsing .xctestrun file and use it to start xctest (#525
Browse files Browse the repository at this point in the history
)

This pull request introduces enhancements to the go-ios tool by adding the ability to parse .xctestrun files, enabling seamless execution of XCTest. The key updates include:

Key updates include:

1. Parsing .xctestrun File:

A new parseFile method has been implemented to extract test configuration details from the .xctestrun file.
The parsed data includes all details needed to execute an xctest like TestHostBundleIdentifier, TestBundlePath, OnlyTestIdentifiers, SkipTestIdentifiers, IsUITestBundle, CommandLineArguments, EnvironmentVariables, and TestingEnvironmentVariables of the xctestrun file.
Added a validation step to ensure the FormatVersion of the .xctestrun file is supported. Currently, only FormatVersion 1 is accepted, with clear error messages for unsupported versions.
2. Enhanced CLI Command:

Introduced a new ios runxctest command in the CLI with updated explanations for better usability.
Allows users to specify xctestrun file path and output preferences.
3. Improved Test Coverage:

Added test cases to validate parsing of .xctestrun file.
Ensured robust assertions for critical fields like TestHostBundleIdentifier and TestBundlePath.
These updates simplify and enhance the process of running XCTest by directly utilizing .xctestrun files, reducing manual setup and ensuring better adaptability to diverse testing scenarios.
  • Loading branch information
bahrimootaz authored Jan 6, 2025
1 parent 4284fc8 commit 0f1771b
Show file tree
Hide file tree
Showing 4 changed files with 534 additions and 4 deletions.
304 changes: 304 additions & 0 deletions ios/testmanagerd/xctestrunnerutils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
package testmanagerd

import (
"github.com/danielpaulus/go-ios/ios"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

// Helper function to create mock data and parse the .xctestrun file
func createAndParseXCTestRunFile(t *testing.T) xCTestRunData {
// Arrange: Create a temporary .xctestrun file with mock data
tempFile, err := os.CreateTemp("", "testfile*.xctestrun")
assert.NoError(t, err, "Failed to create temp file")
defer os.Remove(tempFile.Name()) // Cleanup after test

xcTestRunFileFormatVersion1 := `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>RunnerTests</key>
<dict>
<key>BlueprintName</key>
<string>RunnerTests</string>
<key>BlueprintProviderName</key>
<string>Runner</string>
<key>BlueprintProviderRelativePath</key>
<string>Runner.xcodeproj</string>
<key>BundleIdentifiersForCrashReportEmphasis</key>
<array>
<string>com.example.myApp</string>
<string>com.example.myApp.RunnerTests</string>
</array>
<key>CommandLineArguments</key>
<array/>
<key>DefaultTestExecutionTimeAllowance</key>
<integer>600</integer>
<key>DependentProductPaths</key>
<array>
<string>__TESTROOT__/Release-iphoneos/Runner.app</string>
<string>__TESTROOT__/Release-iphoneos/Runner.app/PlugIns/RunnerTests.xctest</string>
</array>
<key>DiagnosticCollectionPolicy</key>
<integer>1</integer>
<key>EnvironmentVariables</key>
<dict>
<key>APP_DISTRIBUTOR_ID_OVERRIDE</key>
<string>com.apple.AppStore</string>
<key>OS_ACTIVITY_DT_MODE</key>
<string>YES</string>
<key>SQLITE_ENABLE_THREAD_ASSERTIONS</key>
<string>1</string>
<key>TERM</key>
<string>dumb</string>
</dict>
<key>IsAppHostedTestBundle</key>
<true/>
<key>ParallelizationEnabled</key>
<true/>
<key>PreferredScreenCaptureFormat</key>
<string>screenRecording</string>
<key>ProductModuleName</key>
<string>RunnerTests</string>
<key>RunOrder</key>
<integer>0</integer>
<key>SystemAttachmentLifetime</key>
<string>deleteOnSuccess</string>
<key>TestBundlePath</key>
<string>__TESTHOST__/PlugIns/RunnerTests.xctest</string>
<key>TestHostBundleIdentifier</key>
<string>com.example.myApp</string>
<key>TestHostPath</key>
<string>__TESTROOT__/Release-iphoneos/Runner.app</string>
<key>TestLanguage</key>
<string></string>
<key>TestRegion</key>
<string></string>
<key>TestTimeoutsEnabled</key>
<false/>
<key>TestingEnvironmentVariables</key>
<dict>
<key>DYLD_INSERT_LIBRARIES</key>
<string>__TESTHOST__/Frameworks/libXCTestBundleInject.dylib</string>
<key>XCInjectBundleInto</key>
<string>unused</string>
<key>Test</key>
<string>xyz</string>
</dict>
<key>ToolchainsSettingValue</key>
<array/>
<key>UserAttachmentLifetime</key>
<string>deleteOnSuccess</string>
<key>OnlyTestIdentifiers</key>
<array>
<string>TestClass1/testMethod1</string>
<string>TestClass2/testMethod1</string>
</array>
<key>SkipTestIdentifiers</key>
<array>
<string>TestClass1/testMethod2</string>
<string>TestClass2/testMethod2</string>
</array>
<key>IsUITestBundle</key>
<true/>
</dict>
<key>__xctestrun_metadata__</key>
<dict>
<key>ContainerInfo</key>
<dict>
<key>ContainerName</key>
<string>Runner</string>
<key>SchemeName</key>
<string>Runner</string>
</dict>
<key>FormatVersion</key>
<integer>1</integer>
</dict>
</dict>
</plist>
`
_, err = tempFile.WriteString(xcTestRunFileFormatVersion1)
assert.NoError(t, err, "Failed to write mock data to temp file")
tempFile.Close()

// Act: Use the codec to parse the temp file
xcTestRunData, err := parseFile(tempFile.Name())

// Assert: Verify the parsed data
assert.NoError(t, err, "Failed to parse .xctestrun file")
assert.NotNil(t, xcTestRunData, "Parsed data should not be nil")

return xcTestRunData
}

func TestTestHostBundleIdentifier(t *testing.T) {
xcTestRunData := createAndParseXCTestRunFile(t)
assert.Equal(t, "com.example.myApp", xcTestRunData.TestConfig.TestHostBundleIdentifier, "TestHostBundleIdentifier mismatch")
}

func TestTestBundlePath(t *testing.T) {
xcTestRunData := createAndParseXCTestRunFile(t)
assert.Equal(t, "__TESTHOST__/PlugIns/RunnerTests.xctest", xcTestRunData.TestConfig.TestBundlePath, "TestBundlePath mismatch")
}

func TestEnvironmentVariables(t *testing.T) {
xcTestRunData := createAndParseXCTestRunFile(t)
assert.Equal(t, map[string]any{
"APP_DISTRIBUTOR_ID_OVERRIDE": "com.apple.AppStore",
"OS_ACTIVITY_DT_MODE": "YES",
"SQLITE_ENABLE_THREAD_ASSERTIONS": "1",
"TERM": "dumb",
}, xcTestRunData.TestConfig.EnvironmentVariables, "EnvironmentVariables mismatch")
}

func TestTestingEnvironmentVariables(t *testing.T) {
xcTestRunData := createAndParseXCTestRunFile(t)
assert.Equal(t, map[string]any{
"DYLD_INSERT_LIBRARIES": "__TESTHOST__/Frameworks/libXCTestBundleInject.dylib",
"XCInjectBundleInto": "unused",
"Test": "xyz",
}, xcTestRunData.TestConfig.TestingEnvironmentVariables, "TestingEnvironmentVariables mismatch")
}

func TestCommandLineArguments(t *testing.T) {
xcTestRunData := createAndParseXCTestRunFile(t)
assert.Equal(t, []string{}, xcTestRunData.TestConfig.CommandLineArguments, "CommandLineArguments mismatch")
}

func TestOnlyTestIdentifiers(t *testing.T) {
xcTestRunData := createAndParseXCTestRunFile(t)
assert.Equal(t, []string{
"TestClass1/testMethod1",
"TestClass2/testMethod1",
}, xcTestRunData.TestConfig.OnlyTestIdentifiers, "OnlyTestIdentifiers mismatch")
}

func TestSkipTestIdentifiers(t *testing.T) {
xcTestRunData := createAndParseXCTestRunFile(t)
assert.Equal(t, []string{
"TestClass1/testMethod2",
"TestClass2/testMethod2",
}, xcTestRunData.TestConfig.SkipTestIdentifiers, "SkipTestIdentifiers mismatch")
}

func TestFormatVersion(t *testing.T) {
xcTestRunData := createAndParseXCTestRunFile(t)
assert.Equal(t, 1, xcTestRunData.XCTestRunMetadata.FormatVersion, "FormatVersion mismatch")
}

func TestIsUITestBundle(t *testing.T) {
xcTestRunData := createAndParseXCTestRunFile(t)
assert.Equal(t, true, xcTestRunData.TestConfig.IsUITestBundle, "IsUITestBundle mismatch")
}

func TestParseXCTestRunNotSupportedForFormatVersionOtherThanOne(t *testing.T) {
// Arrange: Create a temporary .xctestrun file with mock data
tempFile, err := os.CreateTemp("", "testfile*.xctestrun")
assert.NoError(t, err, "Failed to create temp file")
defer os.Remove(tempFile.Name()) // Cleanup after test

xcTestRunFileFormatVersion2 := `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>__xctestrun_metadata__</key>
<dict>
<key>FormatVersion</key>
<integer>2</integer>
</dict>
</dict>
</plist>
`
_, err = tempFile.WriteString(xcTestRunFileFormatVersion2)
assert.NoError(t, err, "Failed to write mock data to temp file")
tempFile.Close()

// Act: Use the codec to parse the temp file
_, err = parseFile(tempFile.Name())

// Assert the Error Message
assert.Equal(t, "go-ios currently only supports .xctestrun files in formatVersion 1: The formatVersion of your xctestrun file is 2, feel free to open an issue in https://github.com/danielpaulus/go-ios/issues to add support", err.Error(), "Error Message mismatch")
}

// Helper function to create testConfig from parsed mock data
func createTestConfigFromParsedMockData(t *testing.T) (TestConfig, ios.DeviceEntry, *TestListener) {
// Arrange: Create parsed XCTestRunData using the helper function
xcTestRunData := createAndParseXCTestRunFile(t)

// Mock dependencies
mockDevice := ios.DeviceEntry{
DeviceID: 8110,
}
mockListener := &TestListener{}

// Act: Convert XCTestRunData to TestConfig
testConfig, err := xcTestRunData.buildTestConfig(mockDevice, mockListener)

// Assert: Validate the returned TestConfig
assert.NoError(t, err, "Error converting to TestConfig")

return testConfig, mockDevice, mockListener
}

func TestConfigTestRunnerBundleId(t *testing.T) {
testConfig, _, _ := createTestConfigFromParsedMockData(t)
assert.Equal(t, "com.example.myApp", testConfig.TestRunnerBundleId, "TestRunnerBundleId mismatch")
}

func TestConfigXctestConfigName(t *testing.T) {
testConfig, _, _ := createTestConfigFromParsedMockData(t)
assert.Equal(t, "RunnerTests.xctest", testConfig.XctestConfigName, "XctestConfigName mismatch")
}

func TestConfigCommandLineArguments(t *testing.T) {
testConfig, _, _ := createTestConfigFromParsedMockData(t)
assert.Equal(t, []string{}, testConfig.Args, "data mismatch")
}

func TestConfigEnvironmentVariables(t *testing.T) {
testConfig, _, _ := createTestConfigFromParsedMockData(t)
assert.Equal(t, map[string]any{
"APP_DISTRIBUTOR_ID_OVERRIDE": "com.apple.AppStore",
"OS_ACTIVITY_DT_MODE": "YES",
"SQLITE_ENABLE_THREAD_ASSERTIONS": "1",
"TERM": "dumb",
"DYLD_INSERT_LIBRARIES": "__TESTHOST__/Frameworks/libXCTestBundleInject.dylib",
"XCInjectBundleInto": "unused",
"Test": "xyz",
}, testConfig.Env, "EnvironmentVariables mismatch")
}

func TestConfigTestsToRun(t *testing.T) {
testConfig, _, _ := createTestConfigFromParsedMockData(t)
assert.Equal(t, []string{
"TestClass1/testMethod1",
"TestClass2/testMethod1",
}, testConfig.TestsToRun, "TestsToRun mismatch")
}

func TestConfigTestsToSkip(t *testing.T) {
testConfig, _, _ := createTestConfigFromParsedMockData(t)
assert.Equal(t, []string{
"TestClass1/testMethod2",
"TestClass2/testMethod2",
}, testConfig.TestsToSkip, "TestsToSkip mismatch")
}

func TestConfigXcTest(t *testing.T) {
testConfig, _, _ := createTestConfigFromParsedMockData(t)
assert.Equal(t, false, testConfig.XcTest, "XcTest mismatch")
}

func TestConfigDevice(t *testing.T) {
testConfig, mockDevice, _ := createTestConfigFromParsedMockData(t)
assert.Equal(t, mockDevice, testConfig.Device, "Device mismatch")
}

func TestConfigListener(t *testing.T) {
testConfig, _, mockListener := createTestConfigFromParsedMockData(t)
assert.Equal(t, mockListener, testConfig.Listener, "Listener mismatch")
}
Loading

0 comments on commit 0f1771b

Please sign in to comment.