<commit_before>describe KumoDockerCloud::StackFile do

  let(:app_name) { 'application-stack-name' }
  let(:config) { KumoDockerCloud::EnvironmentConfig.new(app_name: app_name, env_name: 'test', config_path: 'a path') }
  let(:env_vars) { {app_name => {'KEY' => 'VALUE'}} }
  let(:plain_text_secrets) do
    {
      'TEST_ENV' => 'FAKE',
      'MORE' => 'ANOTHER'
    }
  end

  before do
    allow(KumoDockerCloud::EnvironmentConfig).to receive(:new).and_return(config)
    allow(config).to receive(:plain_text_secrets).and_return(plain_text_secrets)
  end

  describe '.create_from_template' do
    subject { described_class.create_from_template(stack_template, config, env_vars) }

    let(:stack_template) do
      <<-eos
        application-stack-name:
          image: a-thing
      eos
    end

    it 'adds environment variables to stack config' do
      expect(subject).to eq(app_name => {
        'image' => 'a-thing',
        'environment' => {
          'KEY' => 'VALUE'
        }
      })
    end

    context 'with some existing environment' do
      let(:stack_template) do
        <<-eos
          application-stack-name:
            image: a-thing
            environment:
              TEST: thing
        eos
      end

      it 'should add new secrets to the environment' do
        expect(subject).to eq(app_name => {
          'image' => 'a-thing',
          'environment' => {
            'TEST' => 'thing',
            'KEY' => 'VALUE'
          }
        })
      end

      context 'and environment variables that contain symbols that need special escaping' do
        let(:stack_template) do
          <<-eos
            application-stack-name:
              image: a-thing
              environment:
                TEST: 123$abc
          eos
        end

        # to work around https://github.com/docker/dockercloud-cli/issues/17
        it 'escapes environment variables that contain $ with an extra $' do

          expect(subject).to eq(app_name => {
            'image' => 'a-thing',
            'environment' => {
              'TEST' => '123$$abc',
              'KEY' => 'VALUE'
            }
          })
        end
      end
    end

    context 'without any existing environment' do
      let(:stack_template) do
        <<-eos<commit_msg>Capture failing requirement - numeric keys<commit_after>describe KumoDockerCloud::StackFile do

  let(:app_name) { 'application-stack-name' }
  let(:config) { KumoDockerCloud::EnvironmentConfig.new(app_name: app_name, env_name: 'test', config_path: 'a path') }
  let(:env_vars) { {app_name => {'KEY' => 'VALUE'}} }
  let(:plain_text_secrets) do
    {
      'TEST_ENV' => 'FAKE',
      'MORE' => 'ANOTHER'
    }
  end

  before do
    allow(KumoDockerCloud::EnvironmentConfig).to receive(:new).and_return(config)
    allow(config).to receive(:plain_text_secrets).and_return(plain_text_secrets)
  end

  describe '.create_from_template' do
    subject { described_class.create_from_template(stack_template, config, env_vars) }

    let(:stack_template) do
      <<-eos
        application-stack-name:
          image: a-thing
      eos
    end

    it 'adds environment variables to stack config' do
      expect(subject).to eq(app_name => {
        'image' => 'a-thing',
        'environment' => {
          'KEY' => 'VALUE'
        }
      })
    end

    context 'with some existing environment' do
      let(:stack_template) do
        <<-eos
          application-stack-name:
            image: a-thing
            environment:
              TEST: thing
        eos
      end

      it 'should add new secrets to the environment' do
        expect(subject).to eq(app_name => {
          'image' => 'a-thing',
          'environment' => {
            'TEST' => 'thing',
            'KEY' => 'VALUE'
          }
        })
      end

      context 'and environment variables that contain symbols that need special escaping' do
        let(:stack_template) do
          <<-eos
            application-stack-name:
              image: a-thing
              environment:
                TEST: 123$abc
          eos
        end

        # to work around https://github.com/docker/dockercloud-cli/issues/17
        it 'escapes environment variables that contain $ with an extra $' do

          expect(subject).to eq(app_name => {
            'image' => 'a-thing',
            'environment' => {
              'TEST' => '123$$abc',
              'KEY' => 'VALUE'
            }
          })
        end
      end

      context 'and environment variables that are numeric' do
        let(:stack_template) do
          <<-eos
            application-stack-name:
              image: a-thing
              environment:
                TEST: 555
          eos
        end

        # to work around https://github.com/docker/dockercloud-cli/issues/17
        it 'passes those values through' do

          expect(subject).to eq(app_name => {
            'image' => 'a-thing',
            'environment' => {
              'TEST' => 555,
              'KEY' => 'VALUE'
            }
          })
        end
    end

    context 'without any existing environment' do
      let(:stack_template) do
        <<-eos
{'fraction_non_alphanumeric': 0.11578541103820919, 'fraction_numerical': 0.004631416441528367, 'mean_word_length': 1.815317762085823, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 44, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5382740', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1476, 'n_words': 354}