-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathprogramming_package_spec.rb
More file actions
159 lines (137 loc) · 4.99 KB
/
Copy pathprogramming_package_spec.rb
File metadata and controls
159 lines (137 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
105
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Course::Assessment::ProgrammingPackage do
self::PACKAGE_PATH = File.join(Rails.root,
'spec/fixtures/course/programming_question_template.zip')
self::ADDITIONAL_PACKAGE_PATH = File.join(Rails.root,
'spec/fixtures/course/programming_question_template_with_add_files.zip')
self::EMPTY_PACKAGE_PATH = File.join(Rails.root,
'spec/fixtures/course/'\
'empty_programming_question_template.zip')
def temp_package_path
package_path = Rails.application.config.x.temp_folder.join('spec/packages')
FileUtils.mkdir_p(package_path) unless Dir.exist?(package_path)
Tempfile.create('programming_package', package_path).tap(&:close).path
end
def open_package(path)
Course::Assessment::ProgrammingPackage.new(path)
end
let(:package_path) { self.class::PACKAGE_PATH }
subject { open_package(package_path) }
describe '#initialize' do
context 'when a file path is specified' do
it 'opens the file' do
expect(subject.submission_files).not_to be_empty
end
end
context 'when a File object is specified' do
let(:package_path) { File.new(self.class::PACKAGE_PATH, 'rb') }
it 'opens the file' do
expect(subject.submission_files).not_to be_empty
end
end
context 'when anything else is specified' do
subject { open_package(3) }
it 'fails with ArgumentError' do
expect { subject }.to raise_error(ArgumentError)
end
end
end
describe '#path' do
context 'when the package is not loaded' do
context 'when a File name is given' do
it 'returns the path to the package' do
expect(subject.path).to eq(self.class::PACKAGE_PATH)
end
end
context 'when a File is given' do
let(:package_path) { File.new(self.class::PACKAGE_PATH, 'rb') }
it 'returns the path to the File' do
expect(subject.path).to eq(self.class::PACKAGE_PATH)
end
end
end
context 'when the package is loaded' do
it 'returns the path to the package' do
subject.valid?
expect(subject.path).to eq(self.class::PACKAGE_PATH)
end
end
end
describe '#close' do
let(:package_path) { temp_package_path }
before do
FileUtils.copy(self.class::PACKAGE_PATH, package_path)
end
it 'persists changes to the package' do
subject.submission_files = {}
subject.close
expect(open_package(package_path).submission_files).to be_empty
end
end
describe '#save' do
let(:package_path) { temp_package_path }
before do
FileUtils.copy(self.class::PACKAGE_PATH, package_path)
end
it 'persists changes to the package' do
subject.submission_files = {}
subject.close
expect(open_package(package_path).submission_files).to be_empty
end
end
describe '#valid?' do
let(:package_path) { temp_package_path }
let(:package_to_copy) { self.class::PACKAGE_PATH }
before do
FileUtils.copy(package_to_copy, package_path)
end
context 'when given a valid package' do
it 'returns true' do
expect(subject).to be_valid
end
end
context 'when given an empty package' do
let(:package_to_copy) { self.class::EMPTY_PACKAGE_PATH }
it 'returns false' do
expect(subject).not_to be_valid
end
end
end
describe '#submission_files' do
it 'loads all the submission files' do
expect(subject.submission_files).to eq(Pathname.new('__init__.py') => '')
end
end
describe '#submission_files=' do
it 'replaces all existing submission files' do
replacement = { Pathname.new('test.py') => 'test!' }
expect((subject.submission_files = replacement)).to eq(replacement)
expect(subject.submission_files).to eq(replacement)
end
end
describe '#test_files' do
let!(:package_path) { self.class::ADDITIONAL_PACKAGE_PATH }
it 'loads all the test files' do
expect(subject.test_files.keys).to match_array([Pathname.new('tests_folder_extra_file.py'),
Pathname.new('prepend.py'),
Pathname.new('append.py'),
Pathname.new('autograde.py')])
end
end
describe '#main_files' do
let!(:package_path) { self.class::ADDITIONAL_PACKAGE_PATH }
it 'loads all the main files' do
expect(subject.main_files.keys).to match_array([Pathname.new('root_folder_extra_file.py'),
Pathname.new('Makefile')])
end
end
describe '#ensure_file_open!' do
context 'when the file cannot be opened' do
it 'raises an IllegalStateError' do
subject.instance_variable_set(:@path, nil)
expect { subject.submission_files }.to raise_error(IllegalStateError)
end
end
end
end