Module: Omochi::Util

Includes:
AST::Processor::Mixin
Included in:
CLI
Defined in:
lib/omochi/util.rb

Instance Method Summary collapse

Instance Method Details

#find_spec_file(diff_path) ⇒ Object



61
62
63
64
# File 'lib/omochi/util.rb', line 61

def find_spec_file(diff_path)
  spec_path = File.join('spec', diff_path.gsub(/\.rb$/, '_spec.rb').gsub('app/', ''))
  File.exist?(spec_path) ? spec_path : nil
end

#github_diff_pathObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/omochi/util.rb', line 33

def github_diff_path
  diff_command = 'gh pr diff --name-only'
  diff_output, _diff_error, _diff_status = Open3.capture3(diff_command, chdir: '.')

  # エラーチェック
  unless _diff_status.success?
    puts "Error: Failed to run 'gh pr diff' command."
    return []
  end
  # 取得したdiffのpathを返却する
  diff_output.split("\n")
end

#local_diff_pathObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/omochi/util.rb', line 12

def local_diff_path
  # Gitがインストールされているか確認
  unless system('git --version > /dev/null 2>&1')
    puts 'Error: Git is not installed. Please install Git.'
    return []
  end

  # ローカルのdiffを取得する
  diff_command = 'git diff --diff-filter=d --name-only'
  diff_output, _diff_error, _diff_status = Open3.capture3(diff_command, chdir: '.')

  # エラーチェック
  unless _diff_status.success?
    puts "Error: Failed to run 'git diff' command."
    return []
  end

  # 取得したdiffのpathを返却する
  diff_output.split("\n")
end

#process_missing_spec_file(diff_path, create_spec, perfect) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/omochi/util.rb', line 106

def process_missing_spec_file(diff_path, create_spec, perfect)
  # 対応するSpecファイルが存在しない場合のロジック
  result = {}
  ignored_def_names = get_ignore_methods(diff_path)
  get_ast(diff_path).each do |expr|
    dfs(expr[:ast], expr[:filename], result)
  end
  result = result.transform_keys(&:to_s)

  ignored_def_names.each do |def_name|
    result[def_name] = true if result.key?(def_name)
  end

  if create_spec
    # exprs[0] の AST からメソッド内のコードを生成
    ast_code = get_ast(diff_path)[0][:ast]
    method_code = Unparser.unparse(ast_code)

    puts '==================================================================='
    puts "#{diff_path} のテストを以下に表示します。"
    puts "We will show the test of #{diff_path} below."
    create_spec_by_bedrock(method_code)
  end
  return perfect = false if print_result(diff_path, result).size > 0
end

#process_spec_file(diff_path, create_spec, perfect) ⇒ Object



66
67
68
69
70
71
72
73
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
99
100
101
102
103
104
# File 'lib/omochi/util.rb', line 66

def process_spec_file(diff_path, create_spec, perfect)
  # 対応するSpecファイルが存在した場合のロジック
  result = {}
  spec_def_name_arr = []
  spec_file_path = find_spec_file(diff_path)
  # スペックファイルがあれば、specfileの中身を確認していく。
  # defメソッド名だけ切り出す {:call => {code}, :verify => {code}, ....}
  # ASTを再帰的に探索し、メソッド名とコードを取得
  get_ast(diff_path).each do |expr|
    dfs(expr[:ast], expr[:filename], result)
  end
  result = result.transform_keys(&:to_s)
  # describeメソッド [call]
  # []の中にastが入る
  get_ast(spec_file_path).each do |expr|
    spec_def_name_arr = dfs_describe(expr[:ast], expr[:filename], spec_def_name_arr)
  end

  # resultのHashでSpecが存在するものをTrueに更新
  spec_def_name_arr.each do |spec_def_name|
    next unless result.key?(spec_def_name)

    result[spec_def_name] = true

    next unless create_spec

    method_code = result[spec_def_name]
    puts '==================================================================='
    puts "#{spec_def_name} のテストを以下に表示します。"
    puts "We will show the test of #{spec_def_name} below."
    create_spec_by_bedrock(method_code)
  end

  get_ignore_methods(diff_path).each do |def_name|
    result[def_name] = true if result.key?(def_name)
  end

  return perfect = false if print_result(diff_path, result).size > 0
end

#remote_diff_pathObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/omochi/util.rb', line 46

def remote_diff_path
  # リモートのdiffを取得する
  diff_command = 'git diff --name-only origin/${{ github.event.pull_request.base.ref }}..${{ github.sha }}'
  diff_output, _diff_error, _diff_status = Open3.capture3(diff_command, chdir: '.')

  # エラーチェック
  unless _diff_status.success?
    puts "Error: Failed to run 'git diff' command."
    return []
  end

  # 取得したdiffのpathを返却する
  diff_output.split("\n")
end