alicia
alicia

Reputation: 47

How to Test a Swift Script that's not in a Xcode Project

I started writing a basic swift script file and compiling it as:

swiftc *.swift -o exporter

Script doesn't do anything right now, but will have export, import_files etc. functions. I'm trying to find how I can test this basic script file?

Ideally, I'd have an exporter_test.swift file which I have all my unit tests. Also, it's important to note that I'm fairly new to swift, so not sure if this is doable outside of Xcode.

exporter.swift

func export() -> {
    // will do exporting of data that's been imported in import_files
}

func import_files(files: [String]) -> {
}

Upvotes: 2

Views: 583

Answers (1)

Adrian Sluyters
Adrian Sluyters

Reputation: 2251

You could execute it using the "swift" command line utility:

example:

Create a file (test.swift) with the following contents:

import Foundation
debugPrint("hello!")`

Then from the command prompt just run it with:

swift ./test.swift

and you should get:

13:24 $ swift ./test.swift 
"hello!"

Form within Xcode if you wanted it to execute via the build stages, then you'd have to add a "run script" stage.

  1. In Xcode, click on the project in the Project Navigator (on the left)

enter image description here

  1. On the right (main pane) select the target that needs to have the script run on

enter image description here

  1. Click on the Build Phases tab

enter image description here

  1. Click the + to add a new phase and choose `New Run Script Phase

enter image description here`

  1. Edit the script as required... e.g.

enter image description here

Just note that in 5. above, you'll probably have to rely on the build-time environment variables to reference files. Directly referencing a file i.e. as I did in my example will probably be caught up in a sort of "chroot" where / isn't actually / but rather a folder relating to the build directories.

Upvotes: 2

Related Questions