github.com/saucelabs/saucectl@v0.175.1/internal/saucecloud/xcuitest_test.go (about)

     1  package saucecloud
     2  
     3  import (
     4  	"archive/zip"
     5  	"io"
     6  	"os"
     7  	"path"
     8  	"reflect"
     9  	"testing"
    10  
    11  	"gotest.tools/v3/fs"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  
    15  	"github.com/saucelabs/saucectl/internal/config"
    16  	"github.com/saucelabs/saucectl/internal/xcuitest"
    17  )
    18  
    19  func TestCalculateJobsCount(t *testing.T) {
    20  	runner := &XcuitestRunner{
    21  		Project: xcuitest.Project{
    22  			Xcuitest: xcuitest.Xcuitest{
    23  				App:     "/path/to/app.ipa",
    24  				TestApp: "/path/to/testApp.ipa",
    25  			},
    26  			Suites: []xcuitest.Suite{
    27  				{
    28  					Name: "valid xcuitest project",
    29  					Devices: []config.Device{
    30  						{
    31  							Name:            "iPhone 11",
    32  							PlatformName:    "iOS",
    33  							PlatformVersion: "14.3",
    34  						},
    35  						{
    36  							Name:            "iPhone XR",
    37  							PlatformName:    "iOS",
    38  							PlatformVersion: "14.3",
    39  						},
    40  					},
    41  				},
    42  			},
    43  		},
    44  	}
    45  	assert.Equal(t, runner.calculateJobsCount(runner.Project.Suites), 2)
    46  }
    47  
    48  func TestXcuitestRunner_ensureAppsAreIpa(t *testing.T) {
    49  	dir := fs.NewDir(t, "my-app",
    50  		fs.WithDir("my-app.app",
    51  			fs.WithFile("check-me.txt", "check-me",
    52  				fs.WithMode(0644))),
    53  		fs.WithDir("my-test-app.app",
    54  			fs.WithFile("test-check-me.txt", "test-check-me",
    55  				fs.WithMode(0644))))
    56  	defer dir.Remove()
    57  
    58  	tempDir := fs.NewDir(t, "tmp")
    59  	defer tempDir.Remove()
    60  
    61  	originalAppPath := path.Join(dir.Path(), "my-app.app")
    62  	originalTestAppPath := path.Join(dir.Path(), "my-test-app.app")
    63  
    64  	appPath, err := archive(originalAppPath, tempDir.Path(), ipaArchive)
    65  	if err != nil {
    66  		t.Errorf("got error: %v", err)
    67  	}
    68  	defer os.Remove(appPath)
    69  
    70  	testAppPath, err := archive(originalTestAppPath, tempDir.Path(), ipaArchive)
    71  	if err != nil {
    72  		t.Errorf("got error: %v", err)
    73  	}
    74  	defer os.Remove(testAppPath)
    75  
    76  	if path.Ext(appPath) != ".ipa" {
    77  		t.Errorf("%v: should be an .ipa file", appPath)
    78  	}
    79  	if path.Ext(testAppPath) != ".ipa" {
    80  		t.Errorf("%v: should be an .ipa file", testAppPath)
    81  	}
    82  	checkFileFound(t, appPath, "Payload/my-app.app/check-me.txt", "check-me")
    83  	checkFileFound(t, testAppPath, "Payload/my-test-app.app/test-check-me.txt", "test-check-me")
    84  }
    85  
    86  func checkFileFound(t *testing.T, archiveName, fileName, fileContent string) {
    87  	rd, _ := zip.OpenReader(archiveName)
    88  	defer rd.Close()
    89  
    90  	found := false
    91  	for _, file := range rd.File {
    92  		if file.Name == fileName {
    93  			found = true
    94  			frd, _ := file.Open()
    95  			body, _ := io.ReadAll(frd)
    96  			frd.Close()
    97  			if !reflect.DeepEqual(body, []byte(fileContent)) {
    98  				t.Errorf("want: %v, got: %v", fileContent, body)
    99  			}
   100  		}
   101  	}
   102  	if found == false {
   103  		t.Errorf("%s was not found in archive", fileName)
   104  	}
   105  }