<commit_before>require 'spec_helper'

describe PDK::Util::TemplateURI do
  before :all do
    PDK.answers.update!('template-url' => nil)
  end

  subject(:template_uri) do
    described_class.new(opts_or_uri)
  end

  describe '.new' do
    context 'with a string' do
      let(:opts_or_uri) { 'https://github.com/my/pdk-templates.git#custom' }

      it 'can return a string for storing' do
        expect(template_uri.to_s).to eq('https://github.com/my/pdk-templates.git#custom')
      end
    end
    context 'with an addressable::uri' do
      let(:opts_or_uri) { Addressable::URI.parse('https://github.com/my/pdk-templates.git#custom') }

      it 'can return a string for storing' do
        expect(template_uri.to_s).to eq('https://github.com/my/pdk-templates.git#custom')
      end
    end
    context 'with options' do
      let(:opts_or_uri) do
        {
          :'template-url' => 'https://github.com/my/pdk-templates.git',
          :'template-ref' => 'custom',
        }
      end

      it 'can return a string for storing' do
        allow(PDK::Util::Git).to receive(:repo?).with(anything).and_return(true)
        expect(template_uri.to_s).to eq('https://github.com/my/pdk-templates.git#custom')
      end
    end
  end

  describe '.git_remote' do
    context 'when the uri has a fragment' do
      let(:opts_or_uri) { 'https://github.com/my/pdk-templates.git#custom' }

      it 'returns just the url portion' do
        expect(template_uri.git_remote).to eq 'https://github.com/my/pdk-templates.git'
      end
    end

    context 'when the uri has no fragment' do
      let(:opts_or_uri) { 'https://github.com/my/pdk-templates.git' }

      it 'returns just the url portion' do
        expect(template_uri.git_remote).to eq 'https://github.com/my/pdk-templates.git'
      end
    end

    context 'when the uri is an absolute path' do
      context 'on linux' do
        let(:opts_or_uri) { '/my/pdk-templates.git#custom' }

        it 'returns url portion' do
          allow(Gem).to receive(:win_platform?).and_return(false)
          expect(template_uri.git_remote).to eq '/my/pdk-templates.git'
        end
      end
      context 'on windows' do
        let(:opts_or_uri) { '/C:/my/pdk-templates.git#custom' }

        it 'returns url portion' do
          allow(Gem).to receive(:win_platform?).and_return(true)
          expect(template_uri.git_remote).to eq 'C:/my/pdk-templates.git'
        end
      end
    end
  end

  describe '.git_ref' do
    context 'when the uri has a fragment' do
      let(:opts_or_uri) { 'https://github.com/my/pdk-templates.git#custom' }

      it 'returns just the ref portion' do
        expect(template_uri.git_ref).to eq 'custom'
      end
    end

    context 'when the uri has no fragment' do
      let(:opts_or_uri) { 'https://github.com/my/pdk-templates.git' }

      it 'returns the default ref' do
        expect(template_uri.git_ref).to eq described_class.default_template_ref
      end
    end
  end

  describe '.shell_path' do
    let(:uri) { Addressable::URI.parse(template_url) }

    context 'when the uri has a schema' do
      context 'on linux' do
        let(:template_url) { 'file:///my/pdk-templates.git#fragment' }

        it 'returns the path' do
          allow(Gem).to receive(:win_platform?).and_return(false)
          expect(described_class.template_path(uri)).to eq '/my/pdk-templates.git'
        end
      end

      context 'on windows' do
        let(:template_url) { 'file:///C:/my/pdk-templates.git#fragment' }

        it 'returns the path' do
          allow(Gem).to receive(:win_platform?).and_return(true)
          expect(described_class.template_path(uri)).to eq 'C:/my/pdk-templates.git'
        end
      end
    end

    context 'when the uri is just an absolute path' do
      context 'on linux' do
        let(:template_url) { '/my/pdk-templates.git#custom' }

        it 'returns url portion' do
          allow(Gem).to receive(:win_platform?).and_return(false)
          expect(described_class.template_url(uri)).to eq '/my/pdk-templates.git'
        end
      end
      context 'on windows' do
        let(:template_url) { '/C:/my/pdk-templates.git#custom' }

        it 'returns url portion' do
          allow(Gem).to receive(:win_platform?).and_return(true)
          expect(described_class.template_url(uri)).to eq 'C:/my/pdk-templates.git'
        end
      end<commit_msg>Specs for shell_path<commit_after>require 'spec_helper'

describe PDK::Util::TemplateURI do
  before :all do
    PDK.answers.update!('template-url' => nil)
  end

  subject(:template_uri) do
    described_class.new(opts_or_uri)
  end

  describe '.new' do
    context 'with a string' do
      let(:opts_or_uri) { 'https://github.com/my/pdk-templates.git#custom' }

      it 'can return a string for storing' do
        expect(template_uri.to_s).to eq('https://github.com/my/pdk-templates.git#custom')
      end
    end
    context 'with an addressable::uri' do
      let(:opts_or_uri) { Addressable::URI.parse('https://github.com/my/pdk-templates.git#custom') }

      it 'can return a string for storing' do
        expect(template_uri.to_s).to eq('https://github.com/my/pdk-templates.git#custom')
      end
    end
    context 'with options' do
      let(:opts_or_uri) do
        {
          :'template-url' => 'https://github.com/my/pdk-templates.git',
          :'template-ref' => 'custom',
        }
      end

      it 'can return a string for storing' do
        allow(PDK::Util::Git).to receive(:repo?).with(anything).and_return(true)
        expect(template_uri.to_s).to eq('https://github.com/my/pdk-templates.git#custom')
      end
    end
  end

  describe '.git_remote' do
    context 'when the uri has a fragment' do
      let(:opts_or_uri) { 'https://github.com/my/pdk-templates.git#custom' }

      it 'returns just the url portion' do
        expect(template_uri.git_remote).to eq 'https://github.com/my/pdk-templates.git'
      end
    end

    context 'when the uri has no fragment' do
      let(:opts_or_uri) { 'https://github.com/my/pdk-templates.git' }

      it 'returns just the url portion' do
        expect(template_uri.git_remote).to eq 'https://github.com/my/pdk-templates.git'
      end
    end

    context 'when the uri is an absolute path' do
      context 'on linux' do
        let(:opts_or_uri) { '/my/pdk-templates.git#custom' }

        it 'returns url portion' do
          allow(Gem).to receive(:win_platform?).and_return(false)
          expect(template_uri.git_remote).to eq '/my/pdk-templates.git'
        end
      end
      context 'on windows' do
        let(:opts_or_uri) { '/C:/my/pdk-templates.git#custom' }

        it 'returns url portion' do
          allow(Gem).to receive(:win_platform?).and_return(true)
          expect(template_uri.git_remote).to eq 'C:/my/pdk-templates.git'
        end
      end
    end
  end

  describe '.git_ref' do
    context 'when the uri has a fragment' do
      let(:opts_or_uri) { 'https://github.com/my/pdk-templates.git#custom' }

      it 'returns just the ref portion' do
        expect(template_uri.git_ref).to eq 'custom'
      end
    end

    context 'when the uri has no fragment' do
      let(:opts_or_uri) { 'https://github.com/my/pdk-templates.git' }

      it 'returns the default ref' do
        expect(template_uri.git_ref).to eq described_class.default_template_ref
      end
    end
  end

  describe '.shell_path' do
    context 'when the uri has a schema' do
      context 'on linux' do
        let(:opts_or_uri) { 'file:///my/pdk-templates.git#fragment' }

        it 'returns the path' do
          allow(Gem).to receive(:win_platform?).and_return(false)
          expect(template_uri.shell_path).to eq '/my/pdk-templates.git'
        end
      end

      context 'on windows' do
        let(:opts_or_uri) { 'file:///C:/my/pdk-templates.git#fragment' }

        it 'returns the path' do
          allow(Gem).to receive(:win_platform?).and_return(true)
          expect(template_uri.shell_path).to eq 'C:/my/pdk-templates.git'
        end
      end
    end

    context 'when the uri is just an absolute path' do
      context 'on linux' do
        let(:opts_or_uri) { '/my/pdk-templates.git#custom' }

        it 'returns url portion' do
          allow(Gem).to receive(:win_platform?).and_return(false)
          expect(template_uri.shell_path).to eq '/my/pdk-templates.git'
        end
      end
      context 'on windows' do
        let(:opts_or_uri) { '/C:/my/pdk-templates.git#custom' }

        it 'returns url portion' do
          allow(Gem).to receive(:win_platform?).and_return(true)
          expect(template_uri.shell_path).to eq 'C:/my/pdk-templates.git'
        end
      end
{'fraction_non_alphanumeric': 0.12691466083150985, 'fraction_numerical': 0.0, 'mean_word_length': 2.4973821989528795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 24, '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': '2125129', 'n_tokens_mistral': 2900, 'n_tokens_neox': 2798, 'n_words': 637}