diff_test 0.8.4 → 0.8.6

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: 194963612fbe79dd0506ee1d92e500fca33e8aacf6b5fe38b00cb91741a64367
4
- data.tar.gz: 7a8f7a9a4e516cff513ae27247397ac40d35ed697ca5a483aed94a75d60bc668
3
+ metadata.gz: d912f83be5f069f6801db49f64088b2300053e75927b9b15994d3a69630954ff
4
+ data.tar.gz: 1b7f27298ad240ad3d4f2f9f75257de1ebadc47ffdd7da7656f72318524bd36f
5
5
  SHA512:
6
- metadata.gz: 55bf9771f23900a6f3c0800e090db8dc7884acf9e0432573e25a4e4f75cb5adb20af9f750bb0c04e8a765f91503c4fa10ec559dbb34b77d2802e1a2ffea9c560
7
- data.tar.gz: 6ee7fb4f979941883644d0bf27c29832b613c4e90c66f962beaffe0282b1c9d80761e73009d7da0f823f017c44d8d665ec0a9015222986da1bae53e7c019b03e
6
+ metadata.gz: ac0dba39aa22af19f73911ae8e70d2ba0acb17484f720d39f8f18d355ccb62cd50b4b3d077cb478119f1464eea0a2bdb69c87135d63f76dc02900b61e33ac74d
7
+ data.tar.gz: c50115b9407f7904675e987e930485932cc72a9da2a148361f9ae05e7c996c35171606e98b338ff4533799edfa083282b3634f8455dd9aa6246de24720e4424c
data/README.md CHANGED
@@ -249,3 +249,7 @@ test/services/zoom_webhook_event_processor_test.rb
249
249
 
250
250
  /Users/owaiskhan/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/diff_test-0.8.0/lib/diff_test/test_suite_execution.rb:43: warning: mismatched indentations at 'end' with 'def' at 41
251
251
  /Users/owaiskhan/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/diff_test-0.8.0/lib/diff_test/test_suite_execution.rb:69: warning: assigned but unused variable - response
252
+
253
+
254
+ overhead of const TracePoint and file path prefix native
255
+ method_added/const_added
@@ -5,30 +5,55 @@ module DiffTest
5
5
  include HTTParty
6
6
 
7
7
  def self.get(path, options = {}, &block)
8
- set_api_key_in_headers(options)
8
+ process_options(options)
9
9
  super
10
10
  end
11
11
 
12
12
  def self.post(path, options = {}, &block)
13
- set_api_key_in_headers(options)
13
+ process_options(options)
14
14
  super
15
15
  end
16
16
 
17
17
  def self.put(path, options = {}, &block)
18
- set_api_key_in_headers(options)
18
+ process_options(options)
19
19
  super
20
20
  end
21
21
 
22
22
  def self.patch(path, options = {}, &block)
23
- set_api_key_in_headers(options)
23
+ process_options(options)
24
24
  super
25
25
  end
26
26
 
27
27
  def self.delete(path, options = {}, &block)
28
- set_api_key_in_headers(options)
28
+ process_options(options)
29
29
  super
30
30
  end
31
31
 
32
+ def self.process_options(options)
33
+ jsonify_body(options)
34
+ gzip_body(options)
35
+ set_api_key_in_headers(options)
36
+ end
37
+
38
+ def self.jsonify_body(options)
39
+ return unless options[:body] && options[:body].is_a?(Hash)
40
+ options[:body] = options[:body].to_json
41
+ options[:headers] ||= {}
42
+ options[:headers]['Content-Type'] = 'application/json'
43
+ end
44
+
45
+ def self.gzip_body(options)
46
+ return unless options[:body]
47
+
48
+ buffer = StringIO.new
49
+ gz = Zlib::GzipWriter.new(buffer)
50
+ gz.write(options[:body])
51
+ gz.close
52
+ options[:body] = buffer.string
53
+ options[:headers] ||= {}
54
+ options[:headers]['Content-Encoding'] = 'gzip'
55
+ end
56
+
32
57
  def self.set_api_key_in_headers(options)
33
58
  base_uri DiffTest.configuration.base_api_url
34
59
  options[:headers] ||= {}
@@ -0,0 +1,11 @@
1
+ class BasicObject
2
+ def __basic_object?
3
+ true
4
+ end
5
+ end
6
+
7
+ module Kernel
8
+ def __basic_object?
9
+ false
10
+ end
11
+ end
@@ -66,5 +66,13 @@ module DiffTest
66
66
  def committer_email
67
67
  @committer_email ||= `git log -1 --pretty=%ae`.strip
68
68
  end
69
+
70
+ def bm(*name, &block)
71
+ puts "Starting #{name.join(' ')}------------"
72
+ start = Time.now
73
+ result = yield
74
+ puts "Finished #{name.join(' ')} in #{(Time.now - start).round(2)}s------------"
75
+ result
76
+ end
69
77
  end
70
78
  end
@@ -17,7 +17,11 @@ module DiffTest
17
17
 
18
18
  def save_payload
19
19
  impacted_files.each_with_object({}) do |file_path, payload|
20
- payload[file_path] = FileHashComputer.compute(file_path)
20
+ begin
21
+ payload[file_path] = FileHashComputer.compute(file_path)
22
+ rescue Errno::ENOENT
23
+ nil
24
+ end
21
25
  end
22
26
  end
23
27
 
@@ -7,6 +7,17 @@ module DiffTest
7
7
  end
8
8
 
9
9
  module InstanceMethods
10
+ def relative_test_file_path(runnable_class, runnable_method)
11
+ value = Helper.relative_const_source_path_from_project_root(runnable_class.name) rescue nil
12
+
13
+ unbound_method = runnable_class.instance_method(runnable_method) if runnable_method
14
+ value ||= Helper.relative_path_from_project_root(unbound_method.source_location.first) if unbound_method
15
+
16
+ value
17
+ rescue
18
+ nil
19
+ end
20
+
10
21
  def run
11
22
  return super unless DiffTest.configuration.enabled?
12
23
 
@@ -15,7 +26,7 @@ module DiffTest
15
26
  tests = ::Minitest::Test.runnables.flat_map do |runnable|
16
27
  next if runnable.runnable_methods.empty?
17
28
 
18
- test_file_path = Helper.relative_const_source_path_from_project_root(runnable.name) rescue nil
29
+ test_file_path = relative_test_file_path(runnable, runnable.runnable_methods.first)
19
30
  next unless test_file_path
20
31
 
21
32
  runnable.runnable_methods.map do |test_name|
@@ -39,7 +50,7 @@ module DiffTest
39
50
  return super unless DiffTest::TestSuiteExecution.current.valid?
40
51
 
41
52
  begin
42
- test_file_path = Helper.relative_const_source_path_from_project_root(self.class.name) rescue nil
53
+ test_file_path = relative_test_file_path(self.class, self.name)
43
54
  return super if test_file_path.nil?
44
55
  test_name = self.name
45
56
 
@@ -13,7 +13,7 @@ module DiffTest
13
13
  end
14
14
 
15
15
  def loaded?
16
- supported? && defined?(::Rails)
16
+ supported? && defined?(::Rails) && defined?(::Rails::Railtie)
17
17
  end
18
18
 
19
19
  def integrate
@@ -23,8 +23,8 @@ module DiffTest
23
23
  test: id,
24
24
  result: @result,
25
25
  runtime_ms: runtime_ms,
26
- impacted_files: @impacted_file_tracker.save_payload,
27
- }
26
+ impacted_files: (@impacted_file_tracker.save_payload if passed?),
27
+ }.compact
28
28
  end
29
29
 
30
30
  def id
@@ -62,6 +62,10 @@ module DiffTest
62
62
  @result = :failed
63
63
  end
64
64
 
65
+ def passed?
66
+ @result == :passed
67
+ end
68
+
65
69
  def runtime_ms
66
70
  return 0 if @stopped_at.nil? || @started_at.nil?
67
71
  (@stopped_at - @started_at) * 1000
@@ -9,20 +9,40 @@ module DiffTest
9
9
  end
10
10
 
11
11
  def find_or_create_on_server
12
- response = DiffTest::ApiClient.post("/test_suite_executions", body: payload)
12
+ if ENV['DIFF_TEST_SKIP_API']
13
+ return @id = 'fake-test-suite-execution-id'
14
+ elsif ENV['DIFF_TEST_SKIP_API_CALL']
15
+ payload.to_json
16
+ return @id = 'fake-test-suite-execution-id'
17
+ end
18
+
19
+ response = DiffTest::Helper.bm('create test suite executions') do
20
+ DiffTest::ApiClient.post("/test_suite_executions", body: payload)
21
+ end
22
+
13
23
  raise "Failed to create test suite execution: #{response.inspect}" unless response.success?
14
24
  raise "Failed to create test suite execution: #{response.inspect}" unless response['id'].present?
25
+
15
26
  @id = response['id']
16
27
  end
17
28
 
18
29
  def previous_compatible_test_executions
19
- DiffTest::ApiClient.post("/test_suite_executions/#{id}/previous_compatible_test_executions", body: { tests: @tests })
30
+ if ENV['DIFF_TEST_SKIP_API']
31
+ return {}
32
+ elsif ENV['DIFF_TEST_SKIP_API_CALL']
33
+ { tests: @tests }.to_json
34
+ return {}
35
+ end
36
+
37
+ DiffTest::Helper.bm('previous compatible test executions') do
38
+ DiffTest::ApiClient.post("/test_suite_executions/#{id}/previous_compatible_test_executions", body: { tests: @tests })
39
+ end
20
40
  end
21
41
 
22
42
  def ensure_application_eager_loaded!
23
43
  return if @eager_loaded
24
44
  @eager_loaded = true
25
- ::Rails.application.eager_load! if defined?(::Rails)
45
+ ::Rails.application.eager_load! if defined?(::Rails) && ::Rails.respond_to?(:application)
26
46
  end
27
47
 
28
48
 
@@ -64,9 +84,42 @@ module DiffTest
64
84
  end
65
85
 
66
86
  def save_test_execution_results_on_server
87
+ if ENV['DIFF_TEST_SKIP_API']
88
+ return
89
+ elsif ENV['DIFF_TEST_SKIP_API_CALL']
90
+ @test_executions.map(&:save_payload).to_json
91
+ return
92
+ end
93
+
67
94
  raise "Expected to have at least one test execution" if @test_executions.empty?
68
95
 
69
- response = DiffTest::ApiClient.post("/test_suite_executions/#{id}/test_executions/bulk_create", body: { test_executions: @test_executions.map(&:save_payload) })
96
+ test_executions = @test_executions.map(&:save_payload)
97
+
98
+ impacted_file_hash_and_id_by_path = {}
99
+
100
+ test_executions.each do |test_execution|
101
+ next unless test_execution[:impacted_files]
102
+
103
+ test_execution[:impacted_files].each do |file_path, hash|
104
+ unless impacted_file_hash_and_id_by_path[file_path]
105
+ impacted_file_hash_and_id_by_path[file_path] = [hash, impacted_file_hash_and_id_by_path.size]
106
+ end
107
+ end
108
+
109
+ test_execution[:impacted_file_ids] = test_execution[:impacted_files].keys.map { |file_path| impacted_file_hash_and_id_by_path[file_path][1] }
110
+ test_execution[:impacted_files] = nil
111
+ end
112
+
113
+ impacted_file_path_and_hash_by_id = impacted_file_hash_and_id_by_path.map do |file_path, (hash, id)|
114
+ [id, [file_path, hash]]
115
+ end.to_h
116
+
117
+ DiffTest::Helper.bm('bulk create test executions') do
118
+ DiffTest::ApiClient.post("/test_suite_executions/#{id}/test_executions/bulk_create", body: {
119
+ impacted_file_path_and_hash_by_id: impacted_file_path_and_hash_by_id,
120
+ test_executions: test_executions
121
+ })
122
+ end
70
123
  end
71
124
 
72
125
  def payload
@@ -6,9 +6,10 @@ module DiffTest
6
6
 
7
7
  def initialize
8
8
  super
9
+ do_line_check = ENV['DIFF_TEST_SKIP_LINE_CHECK'] != '1'
9
10
  @seen_constants = Set.new
10
11
  @tracepoint = TracePoint.new(:call, :b_call) do |event|
11
- if DiffTest::Helper.file_in_project?(event.path)
12
+ if do_line_check && DiffTest::Helper.file_in_project?(event.path)
12
13
  record(DiffTest::Helper.relative_path_from_project_root(event.path))
13
14
  end
14
15
 
@@ -17,7 +18,11 @@ module DiffTest
17
18
 
18
19
  unless @seen_constants.include?(constant)
19
20
  @seen_constants.add(constant)
20
- constant = constant.attached_object if constant&.singleton_class?
21
+
22
+ if constant&.singleton_class?
23
+ constant = constant.attached_object if constant&.singleton_class?
24
+ next if constant.__basic_object?
25
+ end
21
26
 
22
27
  path = @@location_by_constant[constant]
23
28
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DiffTest
4
- VERSION = "0.8.4"
4
+ VERSION = "0.8.6"
5
5
  end
data/lib/diff_test.rb CHANGED
@@ -31,6 +31,7 @@ module DiffTest
31
31
  def self.integrate!
32
32
  return unless configuration.integratable?
33
33
 
34
+ require_relative 'diff_test/core_ext'
34
35
  DiffTest::Integrations::RailsJs::Integration.instance.integrate_if_ready
35
36
  DiffTest::Integrations::ActiveRecord::Integration.instance.integrate_if_ready
36
37
  DiffTest::Integrations::Minitest::Integration.instance.integrate_if_ready
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diff_test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4
4
+ version: 0.8.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Owais
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-04-19 00:00:00.000000000 Z
11
+ date: 2025-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -53,6 +53,7 @@ files:
53
53
  - lib/diff_test.rb
54
54
  - lib/diff_test/api_client.rb
55
55
  - lib/diff_test/configuration.rb
56
+ - lib/diff_test/core_ext.rb
56
57
  - lib/diff_test/file_hash_computer.rb
57
58
  - lib/diff_test/helper.rb
58
59
  - lib/diff_test/impacted_file_tracker.rb