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

Add Support for Parsing .xctestrun file and use it to start xctest #525

Merged
merged 26 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4bcc386
Add Support for Parsing .xctestrun file and use it to Start XCTest
bahrimootaz Dec 6, 2024
8e6375b
Add Support for Parsing .xctestrun file and use it to Start XCTest
bahrimootaz Dec 6, 2024
f1f4734
Add Support for Parsing .xctestrun file and use it to Start XCTest
bahrimootaz Dec 6, 2024
8451b6b
Add Support for Parsing .xctestrun file and use it to Start XCTest
bahrimootaz Dec 6, 2024
77167b8
Read all test configs from xctestrun file.
bahrimootaz Dec 6, 2024
6716cf6
Read all test configs from xctestrun file
bahrimootaz Dec 10, 2024
c088386
Merge pull request #1 from bahrimootaz/parse_all_test_config
bahrimootaz Dec 10, 2024
2df460e
fix parsing of the testingEnvVars
bahrimootaz Dec 10, 2024
5b61d8d
use a SchemeData while parsing xctestrun file to host the test configs
bahrimootaz Dec 11, 2024
5d7b9d0
Read all test configs from xctestrun file
bahrimootaz Dec 11, 2024
567570d
Read all test configs from xctestrun file
bahrimootaz Dec 11, 2024
11c4fba
optimize building the TestConfig from the parsed xctestrun file
bahrimootaz Dec 11, 2024
97319ec
Read all test configs from xctestrun file
bahrimootaz Dec 11, 2024
25364bd
Encapsulate the construction of the test config to 'xctestrun' parser…
bahrimootaz Dec 11, 2024
0d94d11
Resolve threads and add unit tests
bahrimootaz Dec 12, 2024
ca2e4f1
refactor testing to make it easy to read
bahrimootaz Dec 12, 2024
c85454c
Read all test configs from xctestrun file
bahrimootaz Dec 12, 2024
a80601d
resolve comments
bahrimootaz Dec 12, 2024
262b817
update comment
bahrimootaz Dec 12, 2024
ad2f45b
update class description
bahrimootaz Dec 12, 2024
4ba1cde
Merge branch 'main' into main
bahrimootaz Dec 12, 2024
6578be9
update variable names for clarity
bahrimootaz Dec 12, 2024
6e3d856
Add unit test for parsing IsUITestBundle from xctestrun file
bahrimootaz Dec 16, 2024
3e1b3d8
Add unit test for parsing IsUITestBundle from xctestrun file
bahrimootaz Dec 17, 2024
9194d63
Add unit test for parsing IsUITestBundle from xctestrun file
bahrimootaz Dec 18, 2024
c584aa8
Merge branch 'danielpaulus:main' into main
bahrimootaz Dec 19, 2024
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
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
Loading