Skip to content

Commit bd970c9

Browse files
committed
Reduce duplication in spec tests by using nesting
This sets up common contexts for an OS and then runs the tests within those. This avoids duplicating the fact definitions.
1 parent b411968 commit bd970c9

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

spec/classes/java_spec.rb

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,54 @@
1010
it { is_expected.to contain_file_line('java-home-environment').with_line('JAVA_HOME=/usr/lib/jvm/java-1.8.0/') }
1111
end
1212

13-
context 'when selecting jdk for Debian Buster (10.0)' do
13+
context 'on Debian Buster (10.0)' do
1414
let(:facts) { { os: { family: 'Debian', name: 'Debian', lsb: { distcodename: 'buster' }, release: { major: '10' }, architecture: 'amd64' } } }
15-
let(:params) { { 'distribution' => 'jdk' } }
1615

17-
it { is_expected.to contain_package('java').with_name('openjdk-11-jdk') }
18-
it { is_expected.to contain_file_line('java-home-environment').with_line('JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/') }
19-
end
16+
context 'when selecting jdk' do
17+
let(:params) { { 'distribution' => 'jdk' } }
2018

21-
context 'when selecting jre for Debian Buster (10.0)' do
22-
let(:facts) { { os: { family: 'Debian', name: 'Debian', lsb: { distcodename: 'buster' }, release: { major: '10' }, architecture: 'amd64' } } }
23-
let(:params) { { 'distribution' => 'jre' } }
19+
it { is_expected.to contain_package('java').with_name('openjdk-11-jdk') }
20+
it { is_expected.to contain_file_line('java-home-environment').with_line('JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/') }
21+
end
22+
23+
context 'when selecting jre' do
24+
let(:params) { { 'distribution' => 'jre' } }
2425

25-
it { is_expected.to contain_package('java').with_name('openjdk-11-jre-headless') }
26-
it { is_expected.to contain_file_line('java-home-environment').with_line('JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/') }
26+
it { is_expected.to contain_package('java').with_name('openjdk-11-jre-headless') }
27+
it { is_expected.to contain_file_line('java-home-environment').with_line('JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/') }
28+
end
2729
end
2830

29-
context 'when selecting jdk for Ubuntu Bionic (18.04)' do
31+
context 'on Ubuntu Bionic (18.04)' do
3032
let(:facts) { { os: { family: 'Debian', name: 'Ubuntu', lsb: { distcodename: 'bionic' }, release: { major: '18.04' }, architecture: 'amd64' } } }
31-
let(:params) { { 'distribution' => 'jdk' } }
3233

33-
it { is_expected.to contain_package('java').with_name('openjdk-11-jdk') }
34-
it { is_expected.to contain_file_line('java-home-environment').with_line('JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/') }
35-
end
34+
context 'when selecting jdk' do
35+
let(:params) { { 'distribution' => 'jdk' } }
3636

37-
context 'when selecting jre for Ubuntu Bionic (18.04)' do
38-
let(:facts) { { os: { family: 'Debian', name: 'Ubuntu', lsb: { distcodename: 'bionic' }, release: { major: '18.04' }, architecture: 'amd64' } } }
39-
let(:params) { { 'distribution' => 'jre' } }
37+
it { is_expected.to contain_package('java').with_name('openjdk-11-jdk') }
38+
it { is_expected.to contain_file_line('java-home-environment').with_line('JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/') }
39+
end
40+
41+
context 'when selecting jre' do
42+
let(:params) { { 'distribution' => 'jre' } }
4043

41-
it { is_expected.to contain_package('java').with_name('openjdk-11-jre-headless') }
42-
it { is_expected.to contain_file_line('java-home-environment').with_line('JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/') }
44+
it { is_expected.to contain_package('java').with_name('openjdk-11-jre-headless') }
45+
it { is_expected.to contain_file_line('java-home-environment').with_line('JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/') }
46+
end
4347
end
4448

45-
context 'when selecting openjdk for Oracle Linux' do
49+
context 'on Oracle Linux 7.0' do
4650
let(:facts) { { os: { family: 'RedHat', name: 'OracleLinux', release: { full: '7.0' }, architecture: 'x86_64' } } }
4751

48-
it { is_expected.to contain_package('java').with_name('java-1.7.0-openjdk-devel') }
49-
end
52+
context 'when selecting openjdk' do
53+
it { is_expected.to contain_package('java').with_name('java-1.7.0-openjdk-devel') }
54+
end
5055

51-
context 'when selecting passed value for Oracle Linux' do
52-
let(:facts) { { os: { family: 'RedHat', name: 'OracleLinux', release: { full: '7.0' }, architecture: 'x86_64' } } }
53-
let(:params) { { 'distribution' => 'jre' } }
56+
context 'when selecting passed value for Oracle Linux' do
57+
let(:params) { { 'distribution' => 'jre' } }
5458

55-
it { is_expected.to contain_package('java').with_name('java-1.7.0-openjdk') }
59+
it { is_expected.to contain_package('java').with_name('java-1.7.0-openjdk') }
60+
end
5661
end
5762

5863
context 'when selecting passed value for Scientific Linux' do

0 commit comments

Comments
 (0)