selective-ruby-core 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e3f8bc085429967202563094660f6d87f134b524893175031ca1ba76ff275e48
4
- data.tar.gz: 785a3e4e67f28c217ed943754b7bdc0fb448f3bab16d6b6bf81b626c5008a7dd
3
+ metadata.gz: c2b97bede5440ad3e033c795b85f85e711abcedf38418304d248e3cb6d7e3c5f
4
+ data.tar.gz: 99e7f0a5424eb2c91b1f970fec49e1b641f4e4533926d03e89899f45468d797b
5
5
  SHA512:
6
- metadata.gz: e95a12181dea9cd4e01b2715b3edd18622f5221f181003cf9a01e59f3d0372e9ea03438dc9580c49a250db23919360e47b1a04f6b7291a51f9ad06aaa67d265d
7
- data.tar.gz: cc228579f4983687626e7350a8732763117f212c7e4c98a94bbdbf95542f4c398f22aabf8527f3b159719ccee1290470d8d1f0427827d760a5e3a8d0aec348b2
6
+ metadata.gz: ad91f66ffac73830b38bc7c6aa3187ce6deb8c073b0c6313d8adfde4e2385e1b2351a3a4baba969729a998d95951b9863a50e15e2bee30b25c374e5b3fa12aed
7
+ data.tar.gz: b8ba80a9327718f13a96ad237d34f4b30ae44c5e14996ba1511bdfe5438b203f9c2615f094fb2b16d8a855652d3bd1175bbe42d07135777556aac6f9f6f4d493
data/lib/bin/build_env.sh CHANGED
@@ -11,6 +11,18 @@ parse_github_slug() {
11
11
  esac
12
12
  }
13
13
 
14
+ # Parse a "group/subgroup/project" path out of a gitlab.com remote URL (SSH or
15
+ # HTTPS). GitLab nests groups, so everything after the host is the path.
16
+ # Self-managed hosts can't be recognized from the URL; GitLab CI says so itself.
17
+ parse_gitlab_path() {
18
+ local url="$1"
19
+ case "$url" in
20
+ git@gitlab.com:*|https://gitlab.com/*|http://gitlab.com/*|ssh://git@gitlab.com/*|https://*@gitlab.com/*)
21
+ echo "$url" | sed -E 's#^(git@|(https?|ssh)://([^@/]+@)?)gitlab\.com[:/]##; s#\.git/?$##; s#/$##'
22
+ ;;
23
+ esac
24
+ }
25
+
14
26
  # Given a git remote URL, classify the provider as github|gitlab|bitbucket or
15
27
  # empty when unrecognized. Used for the git_provider hint.
16
28
  detect_git_provider_from_url() {
@@ -55,6 +67,30 @@ if [ -n "$GITHUB_ACTIONS" ]; then
55
67
  base_sha=""
56
68
  fi
57
69
  fi
70
+ elif [ -n "$GITLAB_CI" ]; then
71
+ platform=gitlab_ci
72
+ branch=${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:-${CI_COMMIT_BRANCH:-$CI_COMMIT_REF_NAME}}
73
+ target_branch=$CI_MERGE_REQUEST_TARGET_BRANCH_NAME
74
+ actor=$GITLAB_USER_LOGIN
75
+ sha=$CI_COMMIT_SHA
76
+ # Merged results pipelines run GitLab's merge commit; this is the branch's own commit.
77
+ head_sha=$CI_MERGE_REQUEST_SOURCE_BRANCH_SHA
78
+ run_id=$CI_PIPELINE_ID
79
+ run_attempt=1
80
+ runner_id=$CI_NODE_INDEX
81
+ pr_title=$CI_MERGE_REQUEST_TITLE
82
+ commit_message=$CI_COMMIT_TITLE
83
+ # CI_COMMIT_AUTHOR is "Name <email>".
84
+ committer_name=$(echo "$CI_COMMIT_AUTHOR" | sed -E 's/ *<[^>]*>$//')
85
+ committer_email=$(echo "$CI_COMMIT_AUTHOR" | sed -nE 's/.*<([^>]*)>$/\1/p')
86
+ git_repo_full_name=$CI_PROJECT_PATH
87
+ git_provider=gitlab
88
+ # Merge request pipelines give the merge base directly; branch pipelines
89
+ # give the commit the push started from.
90
+ base_sha=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}
91
+ if [ "$base_sha" = "0000000000000000000000000000000000000000" ]; then
92
+ base_sha=""
93
+ fi
58
94
  elif [ -n "$CIRCLECI" ]; then
59
95
  platform=circleci
60
96
  branch=$CIRCLE_BRANCH
@@ -66,12 +102,14 @@ elif [ -n "$CIRCLECI" ]; then
66
102
  committer_name=$(git show -s --format='%an' -n 1 $sha)
67
103
  committer_email=$(git show -s --format='%ae' -n 1 $sha)
68
104
  # CircleCI has no single "slug" env var. Compose from username + reponame.
69
- # These work for both GitHub OAuth and GitHub App pipelines. For non-GitHub
70
- # pipelines (GitLab, Bitbucket) the same vars exist but we gate on the repo
71
- # URL to avoid emitting a non-GitHub slug.
105
+ # These work for both GitHub OAuth and GitHub App pipelines. GitLab nests
106
+ # groups, so its path comes from the repo URL instead; other providers
107
+ # (Bitbucket) get no slug.
72
108
  if [ -n "$CIRCLE_PROJECT_USERNAME" ] && [ -n "$CIRCLE_PROJECT_REPONAME" ]; then
73
109
  git_provider=$(detect_git_provider_from_url "$CIRCLE_REPOSITORY_URL")
74
- if [ "$git_provider" = "github" ] || [ -z "$CIRCLE_REPOSITORY_URL" ]; then
110
+ if [ "$git_provider" = "gitlab" ]; then
111
+ git_repo_full_name=$(parse_gitlab_path "$CIRCLE_REPOSITORY_URL")
112
+ elif [ "$git_provider" = "github" ] || [ -z "$CIRCLE_REPOSITORY_URL" ]; then
75
113
  git_repo_full_name="${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"
76
114
  # If we fell through the "no repo URL" path, default provider to github
77
115
  # since the slug composition path is specific to GitHub-style owner/repo.
@@ -94,7 +132,7 @@ elif [ -n "$SEMAPHORE" ]; then
94
132
  committer_name=$(git show -s --format='%an' -n 1 $sha)
95
133
  committer_email=$(git show -s --format='%ae' -n 1 $sha)
96
134
  git_provider=$SEMAPHORE_GIT_PROVIDER
97
- if [ "$git_provider" = "github" ]; then
135
+ if [ "$git_provider" = "github" ] || [ "$git_provider" = "gitlab" ]; then
98
136
  git_repo_full_name=$SEMAPHORE_GIT_REPO_SLUG
99
137
  fi
100
138
  # SEMAPHORE_GIT_COMMIT_RANGE is "<base>...<head>" on push/PR events; take the
@@ -119,6 +157,9 @@ elif [ -n "$BUILDKITE" ]; then
119
157
  if [ "$BUILDKITE_PIPELINE_PROVIDER" = "github" ]; then
120
158
  git_provider=github
121
159
  git_repo_full_name=$(parse_github_slug "$BUILDKITE_REPO")
160
+ elif [ "$BUILDKITE_PIPELINE_PROVIDER" = "gitlab" ]; then
161
+ git_provider=gitlab
162
+ git_repo_full_name=$(parse_gitlab_path "$BUILDKITE_REPO")
122
163
  else
123
164
  git_provider=$BUILDKITE_PIPELINE_PROVIDER
124
165
  fi
@@ -142,6 +183,8 @@ elif [ -n "$RWX" ]; then
142
183
  git_provider=$(detect_git_provider_from_url "$RWX_GIT_REPOSITORY_URL")
143
184
  if [ "$git_provider" = "github" ]; then
144
185
  git_repo_full_name="${RWX_GIT_REPOSITORY_NAME}"
186
+ elif [ "$git_provider" = "gitlab" ]; then
187
+ git_repo_full_name=$(parse_gitlab_path "$RWX_GIT_REPOSITORY_URL")
145
188
  fi
146
189
  elif [ -n "$MINT" ]; then
147
190
  platform=rwx
@@ -160,6 +203,8 @@ elif [ -n "$MINT" ]; then
160
203
  git_provider=$(detect_git_provider_from_url "$MINT_GIT_REPOSITORY_URL")
161
204
  if [ "$git_provider" = "github" ]; then
162
205
  git_repo_full_name="${MINT_GIT_REPOSITORY_NAME}"
206
+ elif [ "$git_provider" = "gitlab" ]; then
207
+ git_repo_full_name=$(parse_gitlab_path "$MINT_GIT_REPOSITORY_URL")
163
208
  fi
164
209
  fi
165
210
 
@@ -171,9 +216,13 @@ if [ -z "$git_repo_full_name" ] && command -v git >/dev/null 2>&1; then
171
216
  origin_url=$(git config --get remote.origin.url 2>/dev/null || true)
172
217
  if [ -n "$origin_url" ]; then
173
218
  parsed_slug=$(parse_github_slug "$origin_url")
219
+ parsed_gitlab=$(parse_gitlab_path "$origin_url")
174
220
  if [ -n "$parsed_slug" ]; then
175
221
  git_repo_full_name="$parsed_slug"
176
222
  [ -z "$git_provider" ] && git_provider=github
223
+ elif [ -n "$parsed_gitlab" ]; then
224
+ git_repo_full_name="$parsed_gitlab"
225
+ [ -z "$git_provider" ] && git_provider=gitlab
177
226
  elif [ -z "$git_provider" ]; then
178
227
  git_provider=$(detect_git_provider_from_url "$origin_url")
179
228
  fi
@@ -29,7 +29,9 @@ module Selective
29
29
  def initialize(runner_class, runner_args, debug: false, log: false)
30
30
  @debug = debug
31
31
  @log = log
32
+ @timings = Timings.new
32
33
  @runner = runner_class.new(runner_args, method(:test_case_callback))
34
+ @timings.mark("runner_ready")
33
35
  @retries = 0
34
36
  @pending_commands = []
35
37
  end
@@ -75,12 +77,11 @@ module Selective
75
77
 
76
78
  private
77
79
 
78
- attr_reader :runner, :pipe, :transport_pid, :retries, :diff
80
+ attr_reader :runner, :pipe, :transport_pid, :retries, :timings
79
81
 
80
82
  # Resolved on first use rather than in the constructor: `selective
81
- # exec` (the plain framework run behind the manifest dry-run) never
82
- # connects, so it must not require the CI environment that naming a
83
- # runner validates.
83
+ # exec` (a plain framework run) never connects, so it must not require
84
+ # the CI environment that naming a runner validates.
84
85
  def runner_id
85
86
  @runner_id ||= safe_filename(get_runner_id)
86
87
  end
@@ -117,7 +118,10 @@ module Selective
117
118
  def next_command
118
119
  return @pending_commands.shift if @pending_commands.any?
119
120
 
120
- JSON.parse(pipe.read, symbolize_names: true)
121
+ started = Timings.monotonic_ms
122
+ message = pipe.read
123
+ timings.waited(Timings.monotonic_ms - started)
124
+ JSON.parse(message, symbolize_names: true)
121
125
  end
122
126
 
123
127
  # One connection: true when the run finished on it, false when it was
@@ -296,6 +300,7 @@ module Selective
296
300
  message = pipe.read
297
301
  response = JSON.parse(message, symbolize_names: true)
298
302
  if response[:command] == "connected"
303
+ timings.mark("connected")
299
304
  @connectivity = true
300
305
  break
301
306
  end
@@ -370,17 +375,19 @@ module Selective
370
375
  end
371
376
 
372
377
  def handle_test_manifest(_data)
378
+ timings.mark("manifest_requested")
373
379
  self.class.restore_reporting!
374
380
  logger.info("Sending Response: test_manifest")
375
- data = {test_cases: runner.manifest["examples"] || runner.manifest["test_cases"]}
376
- if TestMap.tree_requested? && (tree = test_map_tree)
381
+ manifest = timings.measure("manifest_list") { runner.manifest }
382
+ data = {test_cases: manifest["examples"] || manifest["test_cases"]}
383
+ if TestMap.tree_requested? && (tree = timings.measure("manifest_tree") { test_map_tree })
377
384
  data[:test_map_tree] = tree
378
385
  end
379
- num_commits = build_env["num_commits"] || 1000
380
- if (diff = get_diff(num_commits))
386
+ if (diff = timings.measure("manifest_diff") { get_diff })
381
387
  data[:modified_test_files] = modified_test_files(diff)
382
- data[:correlated_files] = correlated_files(diff, num_commits)
383
388
  end
389
+ timings.mark("manifest_sent")
390
+ data[:timings] = timings.startup
384
391
  write({type: "test_manifest", data: data})
385
392
  end
386
393
 
@@ -401,7 +408,7 @@ module Selective
401
408
  end
402
409
 
403
410
  def handle_run_test_cases(data)
404
- runner.run_test_cases(data[:test_case_ids])
411
+ timings.batch { runner.run_test_cases(data[:test_case_ids]) }
405
412
  end
406
413
 
407
414
  # Todo: Rename this command to match the method name
@@ -421,6 +428,7 @@ module Selective
421
428
 
422
429
  with_error_handling do
423
430
  Selective::Ruby::Core::Controller.report_at_finish[:connection_retries] = @retries
431
+ Selective::Ruby::Core::Controller.report_at_finish[:timings] = timings.finish
424
432
  write({type: "report_at_finish", data: Selective::Ruby::Core::Controller.report_at_finish})
425
433
 
426
434
  runner.finish unless exit_status.is_a?(Integer)
@@ -464,13 +472,12 @@ module Selective
464
472
  logger.warn("Test map upload failed: #{e.class}: #{e.message}")
465
473
  end
466
474
 
467
- def correlated_files(diff, num_commits)
468
- Selective::Ruby::Core::FileCorrelator.new(diff, num_commits, build_env["target_branch"]).correlate
469
- end
470
-
471
475
  def test_case_callback(test_case)
472
476
  logger.info("Sending Response: test_case_result: #{test_case[:id]}")
477
+ timings.example(test_case[:run_time])
478
+ started = Timings.monotonic_ms
473
479
  write({type: "test_case_result", data: test_case})
480
+ timings.wrote(Timings.monotonic_ms - started)
474
481
  end
475
482
 
476
483
  def modified_test_files(diff)
@@ -479,11 +486,15 @@ module Selective
479
486
  end
480
487
  end
481
488
 
482
- def get_diff(num_commits)
489
+ # Two-dot diff against the target's tip: comparing trees needs no
490
+ # history, so one commit is enough. A full clone is fetched without a
491
+ # depth, which would otherwise make it shallow.
492
+ def get_diff
483
493
  target_branch = build_env["target_branch"]
484
494
  return if target_branch.nil? || target_branch.empty?
485
495
 
486
- Open3.capture2e("git fetch origin #{target_branch} --depth=#{num_commits}").then do |output, status|
496
+ depth = shallow_repository? ? " --depth=1" : ""
497
+ Open3.capture2e("git fetch origin #{target_branch}#{depth}").then do |output, status|
487
498
  unless status.success?
488
499
  print_warning "Selective was unable to fetch the target branch. This may result in a sub-optimal test order. If the issue persists, please contact support. The output was:\n\n#{output}"
489
500
  return
@@ -500,6 +511,11 @@ module Selective
500
511
  end
501
512
  end
502
513
 
514
+ def shallow_repository?
515
+ output, status = Open3.capture2e("git rev-parse --is-shallow-repository")
516
+ status.success? && output.strip == "true"
517
+ end
518
+
503
519
  def debug?
504
520
  @debug
505
521
  end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Selective
4
+ module Ruby
5
+ module Core
6
+ # Where a runner's time goes, from clock reads at points the controller
7
+ # already passes. Startup marks go out with the manifest; batch totals
8
+ # go out with report_at_finish. All in milliseconds; `*_at` marks are
9
+ # Unix epoch, so the server can line them up with its own.
10
+ class Timings
11
+ attr_reader :marks
12
+
13
+ def initialize(loaded_at_ms: LOADED_AT_MS)
14
+ @marks = {"core_loaded_at" => loaded_at_ms}
15
+ started = self.class.process_started_at_ms
16
+ @marks["process_started_at"] = started if started
17
+ @batches = 0
18
+ @run_ms = 0
19
+ @idle_ms = 0
20
+ @write_ms = 0
21
+ @example_ms = 0.0
22
+ @examples = 0
23
+ end
24
+
25
+ def self.now_ms
26
+ Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)
27
+ end
28
+
29
+ def self.monotonic_ms
30
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
31
+ end
32
+
33
+ # Linux only: the process's start from /proc, so time spent before
34
+ # this gem loaded (bundler, the app's own requires) is visible.
35
+ def self.process_started_at_ms
36
+ ticks = File.read("/proc/self/stat").split(") ").last.split(" ")[19].to_i
37
+ uptime_s = File.read("/proc/uptime").split(" ").first.to_f
38
+ hertz = 100.0
39
+ now_ms - ((uptime_s - ticks / hertz) * 1000).round
40
+ rescue SystemCallError, NoMethodError
41
+ nil
42
+ end
43
+
44
+ def mark(name)
45
+ @marks["#{name}_at"] ||= self.class.now_ms
46
+ end
47
+
48
+ def measure(name)
49
+ started = self.class.monotonic_ms
50
+ yield
51
+ ensure
52
+ @marks["#{name}_ms"] = self.class.monotonic_ms - started
53
+ end
54
+
55
+ # Time spent blocked on the next command, split at the first batch:
56
+ # before it is startup, after it is waiting for work.
57
+ def waited(ms)
58
+ if @batches.zero?
59
+ @marks["wait_before_first_batch_ms"] = (@marks["wait_before_first_batch_ms"] || 0) + ms
60
+ else
61
+ @idle_ms += ms
62
+ end
63
+ end
64
+
65
+ def batch
66
+ mark("first_batch")
67
+ started = self.class.monotonic_ms
68
+ yield
69
+ ensure
70
+ @batches += 1
71
+ @run_ms += self.class.monotonic_ms - started
72
+ mark_last_batch
73
+ end
74
+
75
+ def wrote(ms)
76
+ @write_ms += ms
77
+ end
78
+
79
+ def example(run_time_s)
80
+ @examples += 1
81
+ @example_ms += run_time_s.to_f * 1000
82
+ end
83
+
84
+ def startup
85
+ marks.dup
86
+ end
87
+
88
+ def finish
89
+ {
90
+ "batches" => @batches,
91
+ "examples" => @examples,
92
+ "run_ms" => @run_ms,
93
+ "example_ms" => @example_ms.round,
94
+ "outside_examples_ms" => [@run_ms - @example_ms.round, 0].max,
95
+ "idle_between_batches_ms" => @idle_ms,
96
+ "result_write_ms" => @write_ms
97
+ }.merge(marks.slice("first_batch_at", "last_batch_at", "wait_before_first_batch_ms"))
98
+ end
99
+
100
+ private
101
+
102
+ def mark_last_batch
103
+ @marks["last_batch_at"] = self.class.now_ms
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -3,7 +3,7 @@
3
3
  module Selective
4
4
  module Ruby
5
5
  module Core
6
- VERSION = "0.3.3"
6
+ VERSION = "0.4.0"
7
7
  end
8
8
  end
9
9
  end
@@ -13,6 +13,8 @@ loader.setup
13
13
  module Selective
14
14
  module Ruby
15
15
  module Core
16
+ LOADED_AT_MS = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)
17
+
16
18
  class Error < StandardError; end
17
19
  class ConnectionLostError < StandardError; end
18
20
 
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: selective-ruby-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Wood
8
8
  - Nate Vick
9
+ autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 1980-01-02 00:00:00.000000000 Z
12
+ date: 2026-11-24 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: logger
@@ -63,10 +64,8 @@ files:
63
64
  - ext/selective_tracer/extconf.rb
64
65
  - ext/selective_tracer/selective_tracer.c
65
66
  - lib/bin/build_env.sh
66
- - lib/bin/file_correlation_collector.sh
67
67
  - lib/selective-ruby-core.rb
68
68
  - lib/selective/ruby/core/controller.rb
69
- - lib/selective/ruby/core/file_correlator.rb
70
69
  - lib/selective/ruby/core/helper.rb
71
70
  - lib/selective/ruby/core/stdio_transport.rb
72
71
  - lib/selective/ruby/core/test_map.rb
@@ -74,6 +73,7 @@ files:
74
73
  - lib/selective/ruby/core/test_map/tracer.rb
75
74
  - lib/selective/ruby/core/test_map/tree.rb
76
75
  - lib/selective/ruby/core/test_map/uploader.rb
76
+ - lib/selective/ruby/core/timings.rb
77
77
  - lib/selective/ruby/core/version.rb
78
78
  homepage: https://www.selective.ci
79
79
  licenses:
@@ -82,6 +82,7 @@ metadata:
82
82
  homepage_uri: https://www.selective.ci
83
83
  source_code_uri: http://github.com/selectiveci/selective-ruby-core
84
84
  changelog_uri: https://github.com/selectiveci/selective-ruby-core/blob/main/CHANGELOG.md
85
+ post_install_message:
85
86
  rdoc_options: []
86
87
  require_paths:
87
88
  - lib
@@ -96,7 +97,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
97
  - !ruby/object:Gem::Version
97
98
  version: '0'
98
99
  requirements: []
99
- rubygems_version: 4.0.20
100
+ rubygems_version: 3.5.22
101
+ signing_key:
100
102
  specification_version: 4
101
103
  summary: Selective Ruby Client Core
102
104
  test_files: []
@@ -1,141 +0,0 @@
1
- #!/bin/bash
2
-
3
- # The first argument is the number of commits to process
4
- branch=$1
5
- num_commits=$2
6
-
7
- # Initialize an associative array to hold the files to check
8
- declare -A files_to_check
9
-
10
- # Populate the array with the script arguments, starting from the second argument
11
- for file in "${@:3}"
12
- do
13
- files_to_check["$file"]=1
14
- done
15
-
16
- # Get a list of all commit hashes, in reverse order
17
- all_commits=$(git log origin/$branch --no-merges --format=%H --reverse -n $num_commits)
18
-
19
- # Initialize an associative array to store the test files
20
- declare -A test_files
21
- declare -A uncorrelated_test_files
22
-
23
- # Initialize an array to store the files changed in the previous commit
24
- prev_changed_files=()
25
-
26
- # For each commit...
27
- for commit in $all_commits
28
- do
29
- # Get a list of all files that were changed in the current commit
30
- files=$(git diff-tree --no-commit-id --name-only -r $commit)
31
-
32
- declare -A correlated_test_files
33
-
34
- # # For each file in the list of files changed in the previous commit...
35
- for file in "${prev_changed_files[@]}"
36
- do
37
- # If the file is in the list of files to check...
38
- if [[ ${files_to_check[$file]} ]]; then
39
- # For each file...
40
- for test_file in $files
41
- do
42
- # If the file is in the test/ directory and ends with _test.rb...
43
- if [[ $test_file == spec/*_spec.rb ]]
44
- then
45
- # Increment the count in the associative array
46
- test_files["$file|$test_file"]=$((test_files["$file|$test_file"]+1))
47
- # Add the test file to the correlated_test_files array
48
- correlated_test_files["$test_file"]=1
49
- fi
50
- done
51
- fi
52
- done
53
-
54
- # For each file in the list of files changed in the current commit...
55
- for file in $files
56
- do
57
- # If the file is in the list of files to check...
58
- if [[ ${files_to_check[$file]} ]]; then
59
- # For each file...
60
- for test_file in $files
61
- do
62
- # If the file is in the test/ directory and ends with _test.rb...
63
- if [[ $test_file == spec/*_spec.rb ]]
64
- then
65
- # Increment the count in the associative array
66
- test_files["$file|$test_file"]=$((test_files["$file|$test_file"]+1))
67
- # Add the test file to the correlated_test_files array
68
- correlated_test_files["$test_file"]=1
69
- fi
70
- done
71
- fi
72
- done
73
-
74
- # For each file...
75
- for test_file in $files
76
- do
77
- # If the file is in the test/ directory and ends with _test.rb...
78
- if [[ $test_file == spec/*_spec.rb ]]
79
- then
80
- # If the test file is not correlated to any of the files to check in the current commit...
81
- if [[ -z ${correlated_test_files[$test_file]} ]]
82
- then
83
- # Increment the count in the associative array
84
- uncorrelated_test_files["$test_file"]=$((uncorrelated_test_files["$test_file"]+1))
85
- fi
86
- fi
87
- done
88
-
89
- # Clear the correlated_test_files array for the next commit
90
- unset correlated_test_files
91
-
92
- # Store the list of files changed in this commit for the next iteration
93
- prev_changed_files=($files)
94
- done
95
-
96
- # OUTPUT
97
-
98
- # Initialize an associative array to hold the JSON strings for each file
99
- declare -A file_jsons
100
-
101
- # Add the test_files to the file_jsons associative array
102
- for key in "${!test_files[@]}"
103
- do
104
- file=${key%|*}
105
- test_file=${key#*|}
106
- count=${test_files[$key]}
107
- # Append to the JSON string for this file
108
- file_jsons["$file"]+="\"$test_file\": $count,"
109
- done
110
-
111
- # Initialize an empty string for the test_files JSON
112
- correlated_files_json=""
113
-
114
- # Add the file_jsons to the correlated_files_json string
115
- for file in "${!file_jsons[@]}"
116
- do
117
- # Remove the trailing comma from the JSON string for this file
118
- file_json=${file_jsons[$file]%?}
119
- # Append to the correlated_files_json string
120
- correlated_files_json+="\"$file\": { $file_json },"
121
- done
122
-
123
- # Remove the trailing comma from the correlated_files_json string
124
- correlated_files_json=${correlated_files_json%?}
125
-
126
- # Initialize an empty string for the uncorrelated_test_files JSON
127
- uncorrelated_files_json=""
128
-
129
- # Add the uncorrelated_test_files to the uncorrelated_files_json string
130
- for key in "${!uncorrelated_test_files[@]}"
131
- do
132
- count=${uncorrelated_test_files[$key]}
133
- # Append to the uncorrelated_files_json string
134
- uncorrelated_files_json+="\"$key\": $count,"
135
- done
136
-
137
- # Remove the trailing comma from the uncorrelated_files_json string
138
- uncorrelated_files_json=${uncorrelated_files_json%?}
139
-
140
- # Output the JSON
141
- echo "{ \"correlated_files\": { $correlated_files_json }, \"uncorrelated_files\": { $uncorrelated_files_json } }"
@@ -1,41 +0,0 @@
1
- module Selective
2
- module Ruby
3
- module Core
4
- class FileCorrelator
5
- include Helper
6
-
7
- class FileCorrelatorError < StandardError; end
8
-
9
- FILE_CORRELATION_COLLECTOR_PATH = File.join(ROOT_GEM_PATH, "lib", "bin", "file_correlation_collector.sh")
10
-
11
- def initialize(diff, num_commits, target_branch)
12
- @diff = diff.reject {|f| f =~ /^spec\// }
13
- @num_commits = num_commits
14
- @target_branch = target_branch
15
- end
16
-
17
- def correlate
18
- JSON.parse(get_correlated_files, symbolize_names: true)
19
- rescue FileCorrelatorError, JSON::ParserError
20
- print_warning(<<~MSG)
21
- Selective was unable to correlate the diff to test files. This may result in a sub-optimal test order.
22
- If the issue persists, please contact support.
23
- MSG
24
- end
25
-
26
- private
27
-
28
- attr_reader :diff, :num_commits, :target_branch
29
-
30
- def get_correlated_files
31
- Open3.capture2e("#{FILE_CORRELATION_COLLECTOR_PATH} #{target_branch} #{num_commits} #{diff.join(" ")}").then do |output, status|
32
-
33
- raise FileCorrelatorError unless status.success?
34
-
35
- output
36
- end
37
- end
38
- end
39
- end
40
- end
41
- end