Class: Tes::Request::RSpec::CiSlicer

Inherits:
Object
  • Object
show all
Defined in:
lib/tes/request/rspec/ci_slicer.rb

Constant Summary collapse

SUPPORT_FILE_TYPES =
[:yaml, :yml, :json, :properties]

Instance Method Summary collapse

Constructor Details

#initialize(file_type, project_dir, res_replace_map_json_file = nil, pool_url = nil) ⇒ CiSlicer

Returns a new instance of CiSlicer.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tes/request/rspec/ci_slicer.rb', line 11

def initialize(file_type, project_dir, res_replace_map_json_file = nil, pool_url = nil)
  unless SUPPORT_FILE_TYPES.include?(file_type.to_sym)
    raise(ArgumentError, "Not supported file type:#{file_type}!")
  end

  @cfg_file_type = file_type
  @project_dir = project_dir
  @res_addition_attr_map = (res_replace_map_json_file && JSON.parse(File.read(res_replace_map_json_file)))
  @cfg_target_dir = File.join(@project_dir, '.ci_jobs')
  @pool_url = pool_url
end

Instance Method Details

#get_job_env_profile_str(job, split = ';') ⇒ Object



110
111
112
# File 'lib/tes/request/rspec/ci_slicer.rb', line 110

def get_job_env_profile_str(job, split = ';')
  job[:profile].to_s(split)
end

#get_job_rspec_run_args_str(job, split = ' ') ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/tes/request/rspec/ci_slicer.rb', line 100

def get_job_rspec_run_args_str(job, split = ' ')
  if job[:specs].any? {|s| s.match(/_spec.rb\b/)}
    tags_str = rspec_tag_param_str(job[:tag])
  elsif job[:specs].any? {|s| s.match(/_test.py\b/) or s.match(/test_\w+.py\b/)}
    tags_str = pytest_mark_param_str(job[:tag])
  end
  paths_str = job[:specs].join(split)
  tags_str ? (tags_str + split + paths_str) : paths_str
end

#pytest_mark_param_str(tags) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tes/request/rspec/ci_slicer.rb', line 52

def pytest_mark_param_str(tags)
  case tags
  when Array
    if tags.empty?
      nil
    elsif tags.size > 1
      total_tag_str = tags.map {|t| t =~ /\S+\s+\S+/ ? "(#{t})" : t}.join(' or ')
      "-m '#{total_tag_str}'"
    else
      tag = tags.first
      (tag =~ /\S+\s+\S+/) ? "-m '#{tag}'" : "-m #{tag}"
    end

  when String
    (tags =~ /\S+\s+\S+/) ? "-m '#{tags}'" : "-m #{tags}"
  when nil
    nil
  else
    raise("不支持的类型:#{tags.class}")
  end
end

#rspec_tag_param_str(tags) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tes/request/rspec/ci_slicer.rb', line 39

def rspec_tag_param_str(tags)
  case tags
  when Array
    tags.map {|t| "--tag #{t}"}.join(' ')
  when String
    "--tag #{tags}"
  when nil
    nil
  else
    raise("不支持的类型:#{tags.class}")
  end
end

#run(ci_type, slice_count) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tes/request/rspec/ci_slicer.rb', line 23

def run(ci_type, slice_count)
  puts "Generate RSpec distribute jobs #{@cfg_file_type} file for CI"
  rspec_distribute = Distribute.new(@project_dir)
  pool = if @pool_url
           driver = HTTPClient.new(base_url: @pool_url)
           driver.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
           client = ::Tes::Request::Client.new(driver)
           @pool = client.get_all_res
         else
           {}
         end
  jobs = rspec_distribute.distribute_jobs(ci_type, slice_count, @res_addition_attr_map, pool)
  save_job_files(jobs, @cfg_target_dir, @cfg_file_type)
  @cfg_target_dir
end

#save_job_files(jobs, target_dir, file_type) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tes/request/rspec/ci_slicer.rb', line 74

def save_job_files(jobs, target_dir, file_type)
  unless SUPPORT_FILE_TYPES.include?(file_type)
    raise(ArgumentError, "Not supported file type:#{file_type}!")
  end

  job_configs_for_ci = jobs.map {|j| gen_job_ci_params(j)}
  FileUtils.rm_rf(target_dir)
  FileUtils.mkdir(target_dir)
  case file_type
  when :json
    save_file = File.join(target_dir, 'ci_tasks.json')
    File.open(save_file, 'w') {|f| f.write job_configs_for_ci.to_json}
    puts "Generated #{jobs.size} jobs, Stored in:#{save_file} ."
  when :yml, :yaml
    save_file = File.join(target_dir, 'ci_tasks.yml')
    File.open(save_file, 'w') {|f| f.write job_configs_for_ci.to_yaml}
    puts "Generated #{jobs.size} jobs, Stored in:#{save_file} ."
  when :properties
    job_configs_for_ci.each_with_index do |params, i|
      file = File.join(target_dir, "#{i}.properties")
      JavaProperties.write(params, file)
    end
    puts "Generated #{jobs.size} jobs, Stored in:#{target_dir}/*.properties ."
  end
end