pointfreeco / swift-snapshot-testing

📸 Delightful Swift snapshot testing.
https://www.pointfree.co/episodes/ep41-a-tour-of-snapshot-testing
MIT License
3.74k stars 570 forks source link

Configurable SnapshotDirectory to support Xcode cloud workflow #716

Closed rcarver closed 1 year ago

rcarver commented 1 year ago

Following the thread in #553 it seems there are two steps to using snapshots on Xcode cloud:

  1. Snapshot files must be found in the ci_scripts directory, which is the only set of files available during a test run. This can be done via symlink.
  2. assertSnapshot must be called with a snapshotDirectory argument pointing to the location of the file's snapshots within the ci_scripts directory.

The project I'm working with has many swift packages, many of which use snapshots. I wanted a way to run the snapshot tests on Xcode cloud without changing the local workflow.

This change allows configuration of the snapshot directory via ENV var, with an option specifically for this workflow.


1. Symlink snapshots

This script finds all __Snapshot__ directories and symlinks them into ci_scripts/snapshots. Run it from your project root.

#!/bin/sh
set -e
snapshots=ci_scripts/snapshots
search_path_from_snapshots=../..
rm -rf $snapshots
mkdir $snapshots
cd $snapshots
find $search_path_from_snapshots -type d -name "__Snapshots__" | grep -v .build | while read dir; do
    parent=$(dirname "$dir")
    parent_name=${parent##*/}
    echo $parent_name $dir
    ln -s "$dir" "$parent_name"
done

2. Configure the snapshots location for CI

Set an ENV var to change where the library finds snapshots:

SNAPSHOTTESTING_PACKAGE_PATH=/Volumes/workspace/respository/ci_scripts/snapshots

Note - I'm currently setting this in a Test Plan, but I think it can be set in a Scheme. Ideally in the Xcode cloud Workflow setup but those vars don't seem to be available to the test run?

The result is that snapshots from all packages are flattened and symlinked into the ci_scripts dir, then SnapshotTesting constructs a matching path for assertion when run via CI.

jdowd7 commented 1 year ago

@rcarver used your fork, tried to implement per above... script works great, and my build is as expected. But when I run tests I get the below:

image
rcarver commented 1 year ago

@jdowd7 strange! I actually moved this code into a separate repo if you want to try it that way? Based on that error I'd try a clean build and restart Xcode.

Closing this PR since the standalone approach works well.

jdowd7 commented 1 year ago

@rcarver thanks! I'll give this a try. Drop in meaning manually import and not using SPM?

rcarver commented 1 year ago

@jdowd7 yes use via SPM. It defines a new assertSnapshots function so you should just be able to swap import SnapshotTesting to import SnapshotTestingEnvOverlay