Skip to content

Add test location prefix#248

Merged
malclocke merged 2 commits intomainfrom
te-4849-test-location-prefix
Nov 17, 2025
Merged

Add test location prefix#248
malclocke merged 2 commits intomainfrom
te-4849-test-location-prefix

Conversation

@malclocke
Copy link
Contributor

@malclocke malclocke commented Nov 3, 2025

This change adds a new location_prefix option. If set the specified location_prefix is prepended to the file_name and location of each test when sent to Test Engine.

Example

# spec/spec_helper.rb
Buildkite::TestCollector.configure(
  hook: :rspec,
  # --8<--
  location_prefix: "applications/web_app"
)
$ bundle exec rspec spec/model/user_spec.rb:42
# Data stored in Test Engine:
#  file_name: applications/web_app/spec/model/user_spec.rb
#  location: applications/web_app/spec/model/user_spec.rb:42

The prefix can be set either in the Buildkite::TestCollector.configure call as shown above or by setting BUILDKITE_ANALYTICS_LOCATION_PREFIX in the build environment.

location_prefix is supported by the RSpec, minitest, and Cucumber collector hooks.

- Fix `status:` being ignored
- Allow more options to be passed
- Ensure file_path is a substring of id
@malclocke malclocke requested a review from a team as a code owner November 3, 2025 04:36
@malclocke malclocke force-pushed the te-4849-test-location-prefix branch from 08fa338 to c47422c Compare November 3, 2025 04:36
self.url = url || DEFAULT_URL
self.tracing_enabled = tracing_enabled
self.artifact_path = artifact_path
self.location_prefix = location_prefix || ENV["BUILDKITE_ANALYTICS_LOCATION_PREFIX"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches the env var used for the same purpose in the Javascript collector.


module RSpecExampleTraceHelpers
def fake_example(status:)
example = double("RSpec::Core::Example")
Copy link
Contributor Author

@malclocke malclocke Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The motivation for changing this helper was due to adding some additional tests on file_path and location.

The RSpec library hook looks at the source location of the example, in this case this is the declaration location of the RSpec double method, which is usually buried somewhere obscure like /home/user/path/to/ruby/version/manager/gems/rspec-1.2.3/lib/rspec/double.rb or some such weird path, different in every build environment. This makes testing the correct path and location a little difficult.

I changed this helper to return a verifying instance_double, and additionally allowed injection of the file_path and location from the test to make testing these properties easier.

end
end

class Trace
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new superclass lifts some common behaviour up from each *::Trace implementation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract this class to lib/buildkite/test_collector/trace.rb?

@malclocke malclocke force-pushed the te-4849-test-location-prefix branch from c47422c to 95b7fd3 Compare November 3, 2025 05:05
Allows a file name prefix to be set which will be prepended to
`file_name` and `location` in the test results. Intended for use in
monorepos where the test suite lives in a sub directory of the project.

The change also DRYs some duplicate behaviour by extracting a `Trace`
superclass.
@malclocke malclocke force-pushed the te-4849-test-location-prefix branch from 95b7fd3 to 2615b50 Compare November 9, 2025 21:33
@malclocke malclocke requested a review from nprizal November 9, 2025 21:42
@malclocke malclocke merged commit 1830b4e into main Nov 17, 2025
1 check passed
@malclocke malclocke deleted the te-4849-test-location-prefix branch November 17, 2025 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants