affected_tests 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: af75eb73948c85a496715e0ea5f27610a1edb9ad9db22ddf1529122bc9129fcf
4
+ data.tar.gz: 812ebcd3b665254bfc365e10537896bec4d84bbca4efd17b5de46bfa73ed0346
5
+ SHA512:
6
+ metadata.gz: 90130e1a9102afb5afae71d4b0f14bb024c35ae44f807fd8c3fc75d7f143d14636854c84bb67bebfbd5970bc7326b2968cd2f2997345d86d5be41897ec312ab7
7
+ data.tar.gz: e4d4f3879148330499743228daf4facabd030a4cd799c3a92bb95f99886bb86760d5f77385037fd5b3982c5d7e3546462fc1f4def93eb7ec048e047a2dc52f3b
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at rise.shia@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in affected_tests.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ affected_tests (0.1.0)
5
+ calleree
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ calleree (0.1.0)
11
+ rake (13.0.6)
12
+
13
+ PLATFORMS
14
+ arm64-darwin-21
15
+
16
+ DEPENDENCIES
17
+ affected_tests!
18
+ rake (~> 13.0)
19
+
20
+ BUNDLED WITH
21
+ 2.3.17
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Shia
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # AffectedTests
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/affected_tests`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add affected_tests
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install affected_tests
16
+
17
+ ## Usage
18
+
19
+ ### Aggregate data
20
+
21
+ #### on RSpec
22
+
23
+ on `spec/spec_helper.rb`:
24
+
25
+ ```ruby
26
+ require "affected_tests"
27
+ require "affected_tests/rspec"
28
+
29
+ AffectedTests.setup(
30
+ project_path: File.expand_path("../../", __FILE__),
31
+ test_dir_path: "spec/",
32
+ output_path: "log/affected-tests-map.json"
33
+ )
34
+ ```
35
+
36
+ ### Get Diff
37
+
38
+ #### Schema
39
+
40
+ ```json
41
+ [
42
+ { "filename": "app/models/post.rb", "status": "modified" },
43
+ { "filename": "app/models/comment.rb", "status": "added" },
44
+ { "filename": "app/helpers/something.rb", "status": "deleted" }
45
+ ]
46
+ ```
47
+
48
+ #### From GitHub
49
+
50
+ ```ruby
51
+ client = Octokit::Client.new(access_token: ENV["GITHUB_TOKEN"])
52
+
53
+ targets = client.compare(repository, base_sha, target_sha).files.map do |info|
54
+ {
55
+ filename: info.filename,
56
+ status: info.status
57
+ }
58
+ end
59
+ ```
60
+
61
+ See also: `scripts/generate-diff-files-from-github`
62
+
63
+ ### Calculate affected tests
64
+
65
+ ```ruby
66
+ require "affected_tests/differ"
67
+
68
+ target_tests = AffectedTests::Differ.run(
69
+ diff_file_path: "diff-files.json",
70
+ map_file_path: "affected-tests-map.json"
71
+ )
72
+
73
+ pp target_tests
74
+ # => ["spec/models/post_spec.rb", "spec/requests/posts_spec.rb"]
75
+ ```
76
+
77
+ See also: `scripts/calculate-target-tests`
78
+
79
+ ## Development
80
+
81
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
82
+
83
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
84
+
85
+ ## Contributing
86
+
87
+ Bug reports and pull requests are welcome on GitHub at https://github.com/riseshia/affected_tests. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/riseshia/affected_tests/blob/main/CODE_OF_CONDUCT.md).
88
+
89
+ ## License
90
+
91
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
92
+
93
+ ## Code of Conduct
94
+
95
+ Everyone interacting in the AffectedTests project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/riseshia/affected_tests/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "set"
5
+
6
+ module AffectedTests
7
+ module Differ
8
+ module_function
9
+
10
+ # target calculate policy:
11
+ # codes:
12
+ # added: ignore(if user write test, which covers it)
13
+ # modified: execute associated test
14
+ # deleted: execute associated test
15
+ # tests:
16
+ # added: execute it
17
+ # modified: execute it
18
+ # deleted: ignore
19
+ def run(diff_file_path:, map_file_path:, test_dir_path:)
20
+ diff = JSON.parse(File.read(diff_file_path))
21
+ map_info = JSON.parse(File.read(map_file_path))
22
+ map = map_info["map"]
23
+
24
+ added_codes = []
25
+ modified_codes = []
26
+ deleted_codes = []
27
+
28
+ added_tests = []
29
+ modified_tests = []
30
+ deleted_tests = []
31
+
32
+ diff.each do |diff_info|
33
+ path = diff_info["filename"]
34
+ if path.start_with?(test_dir_path)
35
+ case diff_info["status"]
36
+ when "added"
37
+ added_tests << path
38
+ when "modified"
39
+ modified_tests << path
40
+ when "deleted"
41
+ deleted_tests << path
42
+ end
43
+ else
44
+ case diff_info["status"]
45
+ when "added"
46
+ added_codes << path
47
+ when "modified"
48
+ modified_codes << path
49
+ when "deleted"
50
+ deleted_codes << path
51
+ end
52
+ end
53
+ end
54
+
55
+ target_tests_set = Set.new
56
+
57
+ (modified_codes + deleted_codes).each do |path|
58
+ list = map[path] || []
59
+
60
+ list.each do |suspect_test_path|
61
+ target_tests_set << suspect_test_path
62
+ end
63
+ end
64
+
65
+ (added_tests + modified_tests).each do |path|
66
+ target_tests_set << path
67
+ end
68
+
69
+ target_tests_set.to_a - deleted_tests
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.configure do |config|
4
+ config.before(:suite) do
5
+ AffectedTests.start
6
+ end
7
+
8
+ config.after(:each) do
9
+ target_spec = self.class.declaration_locations.last[0]
10
+ AffectedTests.emit(target_spec)
11
+ end
12
+
13
+ config.after(:suite) do
14
+ AffectedTests.dump
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AffectedTests
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+ require "json"
5
+
6
+ require "calleree"
7
+
8
+ require_relative "affected_tests/version"
9
+
10
+ module AffectedTests
11
+ module_function
12
+
13
+ def setup(project_path:, test_dir_path:, output_path:)
14
+ @project_path = project_path
15
+ @test_dir_path = test_dir_path
16
+ @output_path = output_path
17
+ end
18
+
19
+ def start
20
+ Calleree.start
21
+ end
22
+
23
+ def import_from_calleree(target_test_path, result)
24
+ caller_in_project = result.select do |(caller_info, _callee_info, _count)|
25
+ caller_path = caller_info.first
26
+ target_path?(caller_path)
27
+ end.map do |(caller_info, _callee_info, _count)|
28
+ format_path(caller_info.first)
29
+ end
30
+
31
+ called_in_project = result.select do |(_caller_info, callee_info, _count)|
32
+ callee_path = callee_info.first
33
+ target_path?(callee_path)
34
+ end.map do |(_caller_info, callee_info, _count)|
35
+ format_path(callee_info.first)
36
+ end
37
+
38
+ all_related_paths = (called_in_project + caller_in_project).uniq
39
+
40
+ target_test_file_path = format_path(target_test_path)
41
+
42
+ all_related_paths.each do |path|
43
+ next if path.start_with?(@test_dir_path)
44
+
45
+ if path != target_test_file_path
46
+ add(target_test_file_path, path)
47
+ end
48
+ end
49
+ end
50
+
51
+ def emit(target_test_path)
52
+ res = Calleree.result(clear: true)
53
+ import_from_calleree(target_test_path, res)
54
+ end
55
+
56
+ def dump
57
+ data = { revision: revision, map: cache.transform_values(&:to_a) }
58
+ File.write(@output_path, JSON.dump(data))
59
+ ensure
60
+ Calleree.stop
61
+ end
62
+
63
+ def format_path(path)
64
+ if path&.start_with?(@project_path)
65
+ path.sub(@project_path + "/", "")
66
+ else
67
+ path
68
+ end
69
+ end
70
+
71
+ def add(caller, callee)
72
+ cache[callee] ||= Set.new
73
+ cache[callee].add(caller)
74
+ end
75
+
76
+ def revision
77
+ revision_path = File.expand_path("../../REVISION", __FILE__)
78
+ if File.exist?(revision_path)
79
+ File.read(revision_path).strip
80
+ else
81
+ "UNKNOWN"
82
+ end
83
+ end
84
+
85
+ def cache
86
+ @cache ||= {}
87
+ end
88
+
89
+ def bundler_path
90
+ @bundler_path ||= Bundler.bundle_path.to_s
91
+ end
92
+
93
+ def target_path?(path)
94
+ return false if path.nil?
95
+
96
+ path.start_with?(@project_path) && !path.start_with?(bundler_path)
97
+ end
98
+ end
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "optparse"
4
+
5
+ require "affected_tests/differ"
6
+
7
+ # bin/calculate-target-test --diff-file=<path> --map-file=<path> --test-dir=<path> --delimiter=,
8
+ options = { diff_file_path: nil, map_file_path: nil, delimiter: " " }
9
+
10
+ opt = OptionParser.new
11
+ opt.on("--diff-file=<path>") { |v| options[:diff_file_path] = v }
12
+ opt.on("--map-file=<path>") { |v| options[:map_file_path] = v }
13
+ opt.on("--test-dir=<path>") { |v| options[:test_dir_path] = v }
14
+ opt.on("--output-delimiter=<val>", "Specify delimiter when output target tests. default: whitespace.") {|v| options[:delimiter] = v }
15
+ opt.parse!(ARGV)
16
+
17
+ if options.values.compact.size != 4
18
+ puts "--diff-file, --map-file and --test-dir are required"
19
+ exit 1
20
+ end
21
+
22
+ target_tests = AffectedTests::Differ.run(
23
+ diff_file_path: options[:diff_file_path],
24
+ map_file_path: options[:map_file_path],
25
+ test_dir_path: options[:test_dir_path]
26
+ )
27
+
28
+ # Test all if calculated result is empty
29
+ target_tests = Dir["spec/**/*_spec.rb"] if target_tests.empty?
30
+
31
+ # Split tests files for parallel tests.
32
+ node_number = ENV.fetch("NODE_NUMBER", "1").to_i
33
+ node_index = ENV.fetch("NODE_INDEX", "0").to_i
34
+
35
+ partial_list = target_tests.sort.each_slice(node_number).map { |slice| slice[node_index] }.compact
36
+
37
+ puts partial_list.join(options[:delimiter])
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "json"
4
+
5
+ require "octokit"
6
+
7
+ if ARGV.size != 3
8
+ puts "Help: bin/generate-diff-files <repository> <base_sha> <target_sha>"
9
+ exit 1
10
+ end
11
+
12
+ repository = ARGV.shift
13
+ base_sha = ARGV.shift
14
+ target_sha = ARGV.shift
15
+
16
+ client = Octokit::Client.new(access_token: ENV["GITHUB_TOKEN"])
17
+
18
+ targets = client.compare(repository, base_sha, target_sha).files.map do |info|
19
+ {
20
+ filename: info.filename,
21
+ status: info.status
22
+ }
23
+ end
24
+
25
+ puts JSON.dump(targets)
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: affected_tests
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shia
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: calleree
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Tool-kit for testing based on changed files.
28
+ email:
29
+ - rise.shia@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CODE_OF_CONDUCT.md
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/affected_tests.rb
41
+ - lib/affected_tests/differ.rb
42
+ - lib/affected_tests/rspec.rb
43
+ - lib/affected_tests/version.rb
44
+ - scripts/calculate-target-tests
45
+ - scripts/generate-diff-files-from-github
46
+ homepage: https://github.com/riseshia/affected_tests
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/riseshia/affected_tests
51
+ source_code_uri: https://github.com/riseshia/affected_tests
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.7.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.3.7
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Tool-kit for testing based on changed files.
71
+ test_files: []