<commit_before>require 'spec_helper'
require 'dpl/provider/npm'

describe DPL::Provider::NPM do
  subject :provider do
    described_class.new(DummyContext.new, :email => '<EMAIL>', :api_key => 'test')
  end

  describe "#check_auth" do
    example do
      expect(provider).to receive(:setup_auth)
      expect(provider).to receive(:log).with("Authenticated with email <EMAIL>")
      provider.check_auth
    end
  end

  describe "#push_app" do
    example do
      expect(provider.context).to receive(:shell).with("env NPM_API_KEY=test npm publish")
      expect(FileUtils).to receive(:rm).with(File.expand_path(DPL::Provider::NPM::NPMRC_FILE))
      provider.push_app
    end
  end

  describe "#setup_auth" do
    example do
      test_setup_auth
    end
  end

  context 'when package.json exists' do
    let(:custom_rpm_registry) { 'npm.example.com' }
    before :each do
      expect(File).to receive(:exists?).with('package.json').and_return(true)
    end

    context 'and it defines custom RPM registry' do
      before { expect(File).to receive(:read).with('package.json').and_return("{\"publishConfig\":{\"registry\":\"#{custom_rpm_registry}\"}}") }

      describe '#setup_auth' do
        example do
          test_setup_auth(custom_rpm_registry)
        end
      end
    end

    context 'and it does not define custom RPM registry' do
      before { expect(File).to receive(:read).with('package.json').and_return("{}") }

      describe '#setup_auth' do
        example do
          test_setup_auth
        end
      end
    end
  end
end

def test_setup_auth(registry=DPL::Provider::NPM::DEFAULT_NPM_REGISTRY)
  f = double(:npmrc)
  expect(File).to receive(:open).with(File.expand_path(DPL::Provider::NPM::NPMRC_FILE), 'w').and_return(f)
  expect(f).to receive(:puts).with("//#{registry}/:_authToken=${NPM_API_KEY}")
  provider.setup_auth
end<commit_msg>Account for `#flush` call we added in the previous commit<commit_after>require 'spec_helper'
require 'dpl/provider/npm'

describe DPL::Provider::NPM do
  subject :provider do
    described_class.new(DummyContext.new, :email => '<EMAIL>', :api_key => 'test')
  end

  describe "#check_auth" do
    example do
      expect(provider).to receive(:setup_auth)
      expect(provider).to receive(:log).with("Authenticated with email <EMAIL>")
      provider.check_auth
    end
  end

  describe "#push_app" do
    example do
      expect(provider.context).to receive(:shell).with("env NPM_API_KEY=test npm publish")
      expect(FileUtils).to receive(:rm).with(File.expand_path(DPL::Provider::NPM::NPMRC_FILE))
      provider.push_app
    end
  end

  describe "#setup_auth" do
    example do
      test_setup_auth
    end
  end

  context 'when package.json exists' do
    let(:custom_rpm_registry) { 'npm.example.com' }
    before :each do
      expect(File).to receive(:exists?).with('package.json').and_return(true)
    end

    context 'and it defines custom RPM registry' do
      before { expect(File).to receive(:read).with('package.json').and_return("{\"publishConfig\":{\"registry\":\"#{custom_rpm_registry}\"}}") }

      describe '#setup_auth' do
        example do
          test_setup_auth(custom_rpm_registry)
        end
      end
    end

    context 'and it does not define custom RPM registry' do
      before { expect(File).to receive(:read).with('package.json').and_return("{}") }

      describe '#setup_auth' do
        example do
          test_setup_auth
        end
      end
    end
  end
end

def test_setup_auth(registry=DPL::Provider::NPM::DEFAULT_NPM_REGISTRY)
  f = double(:npmrc)
  expect(File).to receive(:open).with(File.expand_path(DPL::Provider::NPM::NPMRC_FILE), 'w').and_return(f)
  expect(f).to receive(:puts).with("//#{registry}/:_authToken=${NPM_API_KEY}")
  allow(f).to receive(:flush)
  provider.setup_auth
end
{'fraction_non_alphanumeric': 0.13586387434554975, 'fraction_numerical': 0.0, 'mean_word_length': 3.6203143893591294, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, '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': '4860675', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1221, 'n_words': 254}