Skip to content
代码片段 群组 项目
提交 af6aa849 编辑于 作者: David Dieulivol's avatar David Dieulivol 提交者: Rémy Coutable
浏览文件

Extract file writer module to write to output files

上级 e235dbd8
No related branches found
No related tags found
无相关合并请求
......@@ -50,15 +50,17 @@
subject { instance.execute }
context 'when the matching_tests_paths file does not exist' do
before do
allow(File).to receive(:exist?).and_return(false)
allow(File).to receive(:write).with(matching_tests_paths, any_args)
end
let(:instance) { described_class.new(non_existing_output_file, matching_tests_paths) }
let(:non_existing_output_file) { 'tmp/another_file.out' }
it 'creates an empty file' do
expect(File).to receive(:write).with(matching_tests_paths, '')
around do |example|
example.run
ensure
FileUtils.rm_rf(non_existing_output_file)
end
subject
it 'creates the file' do
expect { subject }.to change { File.exist?(non_existing_output_file) }.from(false).to(true)
end
end
......
# frozen_string_literal: true
require 'tempfile'
require_relative '../../../../../tooling/lib/tooling/helpers/file_handler'
class MockClass # rubocop:disable Gitlab/NamespacedClass
include Tooling::Helpers::FileHandler
end
RSpec.describe Tooling::Helpers::FileHandler, feature_category: :tooling do
attr_accessor :input_file_path, :output_file_path
around do |example|
input_file = Tempfile.new('input')
output_file = Tempfile.new('output')
self.input_file_path = input_file.path
self.output_file_path = output_file.path
# See https://ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/
# Tempfile.html#class-Tempfile-label-Explicit+close
begin
example.run
ensure
output_file.close
input_file.close
output_file.unlink
input_file.unlink
end
end
let(:instance) { MockClass.new }
let(:initial_content) { 'previous_content1 previous_content2' }
before do
# We write into the temp files initially, to later check how the code modified those files
File.write(input_file_path, initial_content)
File.write(output_file_path, initial_content)
end
describe '#read_array_from_file' do
subject { instance.read_array_from_file(input_file_path) }
context 'when the input file does not exist' do
let(:non_existing_input_file) { 'tmp/another_file.out' }
subject { instance.read_array_from_file(non_existing_input_file) }
around do |example|
example.run
ensure
FileUtils.rm_rf(non_existing_input_file)
end
it 'creates the file' do
expect { subject }.to change { File.exist?(non_existing_input_file) }.from(false).to(true)
end
end
context 'when the input file is not empty' do
let(:initial_content) { 'previous_content1 previous_content2' }
it 'returns the content of the file in an array' do
expect(subject).to eq(initial_content.split(' '))
end
end
end
describe '#write_array_to_file' do
let(:content_array) { %w[new_entry] }
subject { instance.write_array_to_file(output_file_path, content_array) }
context 'when the output file does not exist' do
let(:non_existing_output_file) { 'tmp/another_file.out' }
subject { instance.write_array_to_file(non_existing_output_file, content_array) }
around do |example|
example.run
ensure
FileUtils.rm_rf(non_existing_output_file)
end
it 'creates the file' do
expect { subject }.to change { File.exist?(non_existing_output_file) }.from(false).to(true)
end
end
context 'when the output file is empty' do
let(:initial_content) { '' }
it 'writes the correct content to the file' do
expect { subject }.to change { File.read(output_file_path) }.from('').to(content_array.join(' '))
end
end
context 'when the output file is not empty' do
let(:initial_content) { 'previous_content1 previous_content2' }
it 'appends the correct content to the file' do
expect { subject }.to change { File.read(output_file_path) }
.from(initial_content)
.to((initial_content.split(' ') + content_array).join(' '))
end
end
end
end
......@@ -52,8 +52,8 @@
end
context 'when no partials were modified' do
it 'empties the output file' do
expect { subject }.to change { File.read(output_file) }.from(output_file_content).to('')
it 'does not change the output file' do
expect { subject }.not_to change { File.read(output_file) }
end
end
......@@ -76,8 +76,8 @@
File.write("#{view_base_folder}/my_view.html.haml", "render 'another_partial'")
end
it 'empties the output file' do
expect { subject }.to change { File.read(output_file) }.from(output_file_content).to('')
it 'does not change the output file' do
expect { subject }.not_to change { File.read(output_file) }
end
end
......@@ -89,7 +89,7 @@
it 'writes the view including the partial to the output' do
expect { subject }.to change { File.read(output_file) }
.from(output_file_content)
.to("#{view_base_folder}/my_view.html.haml")
.to(output_file_content + " #{view_base_folder}/my_view.html.haml")
end
end
end
......
# frozen_string_literal: true
require 'test_file_finder'
require_relative 'helpers/file_handler'
module Tooling
class FindTests
include Helpers::FileHandler
def initialize(changes_file, matching_tests_paths)
@matching_tests_paths = matching_tests_paths
@changed_files = File.read(changes_file).split(' ')
File.write(matching_tests_paths, '') unless File.exist?(matching_tests_paths)
@matching_tests = File.read(matching_tests_paths).split(' ')
@changed_files = read_array_from_file(changes_file)
end
def execute
......@@ -22,8 +21,7 @@ def execute
end
end
new_matching_tests = tff.test_files.uniq
File.write(matching_tests_paths, (matching_tests + new_matching_tests).join(' '))
write_array_to_file(matching_tests_paths, tff.test_files.uniq)
end
private
......
# frozen_string_literal: true
require 'fileutils'
module Tooling
module Helpers
module FileHandler
def read_array_from_file(file)
FileUtils.touch file
File.read(file).split(' ')
end
def write_array_to_file(file, content_array)
FileUtils.touch file
output_content = (File.read(file).split(' ') + content_array).join(' ')
File.write(file, output_content)
end
end
end
end
# frozen_string_literal: true
require_relative '../../../../lib/gitlab_edition'
require_relative '../helpers/file_handler'
# Returns system specs files that are related to the JS files that were changed in the MR.
module Tooling
module Mappings
class Base
include Helpers::FileHandler
# Input: A list of space-separated files
# Output: A list of space-separated specs files (JS, Ruby, ...)
def execute(changed_files)
......
......@@ -9,7 +9,7 @@ module Mappings
class PartialToViewsMappings < Base
def initialize(changes_file, output_file, view_base_folder: 'app/views')
@output_file = output_file
@changed_files = File.read(changes_file).split(' ')
@changed_files = read_array_from_file(changes_file)
@view_base_folders = folders_for_available_editions(view_base_folder)
end
......@@ -28,7 +28,7 @@ def execute
end
end
File.write(output_file, views_including_modified_partials.join(' '))
write_array_to_file(output_file, views_including_modified_partials)
end
def filter_files
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册