Skip to content

Commit

Permalink
Run tests using previously created simulator if possible
Browse files Browse the repository at this point in the history
This only applies if there is no destination configured on '.batler.yml'.
If the stored simulator does not exist, a new one is created.
If there is no simulator stored in cache, a new one is created.
  • Loading branch information
vitorbaraujo committed Dec 3, 2020
1 parent 755fa45 commit b588297
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions simctl/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ package simctl

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
)

const simulatorCacheFilename = "/tmp/batler-simulator"

// CreateSimulator creates an arbitrary iOS simulator from existing runtimes and devicetypes.
// This method returns the identifier for the newly created simulator or an error, otherwise.
func CreateSimulator(xcodePath string) (string, error) {
cachedSimulatorID, err := getSimulatorFromCache(xcodePath)
if err == nil {
return cachedSimulatorID, nil
}

runtimes, err := ListRuntimes(xcodePath)
if err != nil {
return "", fmt.Errorf("could not get runtimes: %w", err)
Expand All @@ -34,13 +42,50 @@ func CreateSimulator(xcodePath string) (string, error) {
}

simulatorID := strings.TrimRight(string(output), "\r\n")

if err := saveSimulatorOnCache(simulatorID); err != nil {
// TODO (vitor.araujo): replace fmt by logging
fmt.Printf("failed to cache simulator id: %v\n", err)
}

return simulatorID, nil
}
}

return "", fmt.Errorf("could not create simulator from device types and runtimes")
}

func saveSimulatorOnCache(simulatorID string) error {
return ioutil.WriteFile(simulatorCacheFilename, []byte(simulatorID), 0600)
}

func getSimulatorFromCache(xcodePath string) (string, error) {
output, err := ioutil.ReadFile(simulatorCacheFilename)
if err != nil {
return "", fmt.Errorf("could not read cache file: %w", err)
}

simulatorID := strings.TrimRight(string(output), "\r\n")
if !simulatorExists(simulatorID, xcodePath) {
return "", fmt.Errorf("cached simulator with ID=%s does not exist", simulatorID)
}

return simulatorID, nil
}

func simulatorExists(deviceID, xcodePath string) bool {
cmd := exec.Command("xcrun", "simctl", "list", "devices", deviceID)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("DEVELOPER_DIR=%s", xcodePath))

output, err := cmd.Output()
if err != nil {
return false
}

return strings.Contains(string(output), deviceID)
}

func createDeviceName(runtimeID, deviceTypeID string) string {
runtimeIDParts := strings.Split(runtimeID, ".")
osVersion := runtimeIDParts[len(runtimeIDParts)-1]
Expand Down

0 comments on commit b588297

Please sign in to comment.