Setting up environment variables or other way to configure secrets for tests, in a Swift Package

Thanks for viewing my previous questions and replying :) I seem to keep asking questions I can't find any answers to. I appreciate your thoughtful responses

I have a test target in my Swift Package called SystemTests. It needs some credentials (API key and token for a few services) at runtime in order to run the tests. Does anyone have suggestions on how I can do this without committing this file to git or making the process of running tests tedious.

  • Keeping the environment variable in the Xcode scheme means the API key is stored in xcscheme xml file, and would be difficult to keep out of git. This is accessible through ProcessInfo.processInfo.environment["PRIVATE_API_KEY"]. Unfortunately system environment variables are not available. I'd love to be able to set the environment variables in e.g. ~/.zshrc
  • Current/ flawed approach: I'm currently using a Credentials.swift/ Secrets.swift file which is added to .gitignore and import it to the test file. I have an Example.Secrets.swift file which other developers can use. To avoid namespace collision between the global variables, I use struct ExampleSecrets and struct Secrets, for example:
struct ExampleSecrets {
    let keyNameForServiceA = "put your key here"
    let accessTokenForServiceB = "put your access token here"

Users need to rename ExampleSecrets to Secrets before it would compile.

The disadvantage of this is I can't share this Secrets file between targets/ test targets, because the Swift compiler will detect this. I can use a symlink to hack it together.

Terms of Service

Privacy Policy

Cookie Policy