|
| 1 | +# SPDX-License-Identifier: BSD-2-Clause |
| 2 | +# |
| 3 | +# configuration_spec.rb |
| 4 | +# Part of NetDEF CI System |
| 5 | +# |
| 6 | +# Copyright (c) 2025 by |
| 7 | +# Network Device Education Foundation, Inc. ("NetDEF") |
| 8 | +# |
| 9 | +# frozen_string_literal: true |
| 10 | + |
| 11 | +describe GitHubApp::Configuration do |
| 12 | + let(:config_path) { File.expand_path('config.yml', File.join(File.dirname(__FILE__), '../../../')) } |
| 13 | + let(:default_config) do |
| 14 | + { |
| 15 | + 'debug' => true, |
| 16 | + 'ci' => { 'url' => 'https://custom.ci.url' } |
| 17 | + } |
| 18 | + end |
| 19 | + |
| 20 | + before do |
| 21 | + allow(YAML).to receive(:load_file).and_return(default_config) |
| 22 | + if described_class.instance_variable_defined?(:@singleton__instance__) |
| 23 | + described_class.__send__(:remove_instance_variable, :@singleton__instance__) |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + describe '.instance' do |
| 28 | + it 'returns a singleton instance' do |
| 29 | + expect(described_class.instance).to be_a(described_class) |
| 30 | + expect(described_class.instance).to equal(described_class.instance) |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + describe '#config' do |
| 35 | + it 'loads the configuration from YAML' do |
| 36 | + expect(described_class.instance.config).to eq(default_config) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + describe '#debug?' do |
| 41 | + context 'when debug is true' do |
| 42 | + it 'returns true' do |
| 43 | + expect(described_class.instance.debug?).to be true |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + context 'when debug is false' do |
| 48 | + before do |
| 49 | + allow(YAML).to receive(:load_file).and_return(default_config.merge('debug' => false)) |
| 50 | + end |
| 51 | + |
| 52 | + it 'returns false' do |
| 53 | + expect(described_class.instance.debug?).to be false |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + context 'when debug key is missing' do |
| 58 | + before do |
| 59 | + allow(YAML).to receive(:load_file).and_return(default_config.except('debug')) |
| 60 | + end |
| 61 | + |
| 62 | + it 'returns false' do |
| 63 | + expect(described_class.instance.debug?).to be false |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + describe '#ci_url' do |
| 69 | + context 'when ci url is present' do |
| 70 | + it 'returns the configured ci url' do |
| 71 | + expect(described_class.instance.ci_url).to eq('https://custom.ci.url') |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + context 'when ci key is missing' do |
| 76 | + before do |
| 77 | + allow(YAML).to receive(:load_file).and_return(default_config.except('ci')) |
| 78 | + end |
| 79 | + |
| 80 | + it 'returns the default ci url' do |
| 81 | + expect(described_class.instance.ci_url).to eq('https://ci1.netdef.org') |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context 'when ci url is missing' do |
| 86 | + before do |
| 87 | + allow(YAML).to receive(:load_file).and_return(default_config.merge('ci' => {})) |
| 88 | + end |
| 89 | + |
| 90 | + it 'returns the default ci url' do |
| 91 | + expect(described_class.instance.ci_url).to eq('https://ci1.netdef.org') |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + describe '#reload' do |
| 97 | + it 'reloads the configuration' do |
| 98 | + expect(YAML).to receive(:load_file).twice.and_return(default_config) |
| 99 | + instance = described_class.instance |
| 100 | + instance.reload |
| 101 | + end |
| 102 | + end |
| 103 | +end |
0 commit comments