<commit_before>from __future__ import unicode_literals

import os
import textwrap
import sys
import subprocess

from rwt import scripts


def test_pkg_imported(tmpdir):
	"""
	Create a script that loads cython and ensure it runs.
	"""
	body = textwrap.dedent("""
		import path
		print("Successfully imported path.py")
		""").lstrip()
	script_file = tmpdir / 'script'
	script_file.write_text(body, 'utf-8')
	pip_args = ['path.py']
	cmd = [sys.executable, '-m', 'rwt'] + pip_args + ['--', str(script_file)]

	out = subprocess.check_output(cmd, universal_newlines=True)
	assert 'Successfully imported path.py' in out


class TestDepsReader:
	def test_reads_files_with_attribute_assignment(self):
		script = textwrap.dedent('''
			__requires__=['foo']
			x.a = 'bar'
			''')
		assert scripts.DepsReader(script).read() == ['foo']

	def test_reads_files_with_multiple_assignment(self):
		script = textwrap.dedent('''
			__requires__=['foo']
			x, a = [a, x]
			''')
		assert scripts.DepsReader(script).read() == ['foo']

	def test_single_dep(self):
		script = textwrap.dedent('''
			__requires__='foo'
			''')
		assert scripts.DepsReader(script).read() == ['foo']

	def test_index_url(self):
		script = textwrap.dedent('''
			__requires__ = ['foo']
			__index_url__ = 'https://my.private.index/'
			''')
		reqs = scripts.DepsReader(script).read()
		assert reqs.index_url == 'https://my.private.index/'

	def test_dependency_links(self):
		script = textwrap.dedent('''
			__requires__ = ['foo==0.42']
			__dependency_links__ = ['git+ssh://git@example.com/repo.git#egg=foo-0.42']
			''')
		reqs = scripts.DepsReader(script).read()
		assert reqs.dependency_links == [
			'git+ssh://git@example.com/repo.git#egg=foo-0.42']


def test_pkg_loaded_from_alternate_index(tmpdir):
	"""
	Create a script that loads cython from an alternate index
	and ensure it runs.
	"""
	body = textwrap.dedent("""
		__requires__ = ['path.py']
		__index_url__ = 'https://devpi.net/root/pypi/+simple/'
		import path
		print("Successfully imported path.py")
		""").lstrip()
	script_file = tmpdir / 'script'
	script_file.write_text(body, 'utf-8')<commit_msg>Add test capturing desired behavior. Ref #30.<commit_after>from __future__ import unicode_literals

import os
import textwrap
import sys
import subprocess

from rwt import scripts


def test_pkg_imported(tmpdir):
	"""
	Create a script that loads cython and ensure it runs.
	"""
	body = textwrap.dedent("""
		import path
		print("Successfully imported path.py")
		""").lstrip()
	script_file = tmpdir / 'script'
	script_file.write_text(body, 'utf-8')
	pip_args = ['path.py']
	cmd = [sys.executable, '-m', 'rwt'] + pip_args + ['--', str(script_file)]

	out = subprocess.check_output(cmd, universal_newlines=True)
	assert 'Successfully imported path.py' in out


class TestDepsReader:
	def test_reads_files_with_attribute_assignment(self):
		script = textwrap.dedent('''
			__requires__=['foo']
			x.a = 'bar'
			''')
		assert scripts.DepsReader(script).read() == ['foo']

	def test_reads_files_with_multiple_assignment(self):
		script = textwrap.dedent('''
			__requires__=['foo']
			x, a = [a, x]
			''')
		assert scripts.DepsReader(script).read() == ['foo']

	def test_single_dep(self):
		script = textwrap.dedent('''
			__requires__='foo'
			''')
		assert scripts.DepsReader(script).read() == ['foo']

	def test_index_url(self):
		script = textwrap.dedent('''
			__requires__ = ['foo']
			__index_url__ = 'https://my.private.index/'
			''')
		reqs = scripts.DepsReader(script).read()
		assert reqs.index_url == 'https://my.private.index/'

	def test_dependency_links(self):
		script = textwrap.dedent('''
			__requires__ = ['foo==0.42']
			__dependency_links__ = ['git+ssh://git@example.com/repo.git#egg=foo-0.42']
			''')
		reqs = scripts.DepsReader(script).read()
		assert reqs.dependency_links == [
			'git+ssh://git@example.com/repo.git#egg=foo-0.42']

	def test_fstrings_allowed(self):
		"""
		It should be possible to read dependencies from a script
		with f-strings on all Pythons.
		"""
		script = textwrap.dedent('''
			__requires__ = 'foo'
			f'boo'
			''')
		reqs = scripts.DepsReader(script).read()
		assert reqs == ['foo']


def test_pkg_loaded_from_alternate_index(tmpdir):
	"""
	Create a script that loads cython from an alternate index
	and ensure it runs.
	"""
	body = textwrap.dedent("""
		__requires__ = ['path.py']
		__index_url__ = 'https://devpi.net/root/pypi/+simple/'
		import path
		print("Successfully imported path.py")
		""").lstrip()
	script_file = tmpdir / 'script'
	script_file.write_text(body, 'utf-8')
{'fraction_non_alphanumeric': 0.15978928884986832, 'fraction_numerical': 0.0052677787532923615, 'mean_word_length': 4.444444444444445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '326029', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1613, 'n_words': 330}