<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> import shutil <EOL> import tempfile <EOL> import unittest <EOL> from pants . reporting . linkify import linkify <EOL> def ensure_dir_exists ( path ) : <EOL> os . makedirs ( path ) <EOL> def ensure_file_exists ( path ) : <EOL> ensure_dir_exists ( os . path . dirname ( path ) ) <EOL> open ( path , '<STR_LIT:a>' ) . close ( ) <EOL> class LinkifyTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . _buildroot = tempfile . mkdtemp ( prefix = '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> if os . path . exists ( self . _buildroot ) : <EOL> shutil . rmtree ( self . _buildroot , ignore_errors = True ) <EOL> def _do_test_linkify ( self , expected_link , url , memo = None ) : <EOL> memo = { } if memo is None else memo <EOL> s = '<STR_LIT>' . format ( url ) <EOL> expected = '<STR_LIT>' . format ( expected_link , url ) <EOL> linkified = linkify ( self . _buildroot , s , memo ) <EOL> self . assertEqual ( expected , linkified ) <EOL> def _do_test_not_linkified ( self , url , memo = None ) : <EOL> memo = { } if memo is None else memo <EOL> s = '<STR_LIT>' . format ( url ) <EOL> linkified = linkify ( self . _buildroot , s , memo ) <EOL> self . assertEqual ( s , linkified ) <EOL> def test_linkify_absolute_paths ( self ) : <EOL> relpath = '<STR_LIT>' <EOL> path = os . path . join ( self . _buildroot , relpath ) <EOL> ensure_file_exists ( path ) <EOL> self . _do_test_linkify ( '<STR_LIT>' . format ( relpath ) , path ) <EOL> def test_linkify_relative_paths ( self ) : <EOL> relpath = '<STR_LIT>' <EOL> path = os . path . join ( self . _buildroot , relpath ) <EOL> ensure_file_exists ( path ) <EOL> self . _do_test_linkify ( '<STR_LIT>' . format ( relpath ) , relpath ) <EOL> def test_linkify_relative_path_outside_buildroot ( self ) : <EOL> self . _do_test_not_linkified ( '<STR_LIT>' ) <EOL> def test_linkify_non_existent_relative_paths ( self ) : <EOL> relpath = '<STR_LIT>' <EOL> self . _do_test_not_linkified ( relpath ) <EOL> def test_linkify_http ( self ) : <EOL> url = '<STR_LIT>' <EOL> self . _do_test_linkify ( url , url ) <EOL> url = '<STR_LIT>' <EOL> self . _do_test_linkify ( url , url ) <EOL> def test_linkify_https ( self ) : <EOL> url = '<STR_LIT>' <EOL> self . _do_test_linkify ( url , url ) <EOL> def test_linkify_sftp ( self ) : <EOL> url = '<STR_LIT>' <EOL> self . _do_test_not_linkified ( url ) <EOL> def test_linkify_target ( self ) : <EOL> ensure_file_exists ( os . path . join ( self . _buildroot , '<STR_LIT>' ) ) <EOL> self . _do_test_linkify ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . _do_test_linkify ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_linkify_suffix ( self ) : <EOL> ensure_file_exists ( os . path . join ( self . _buildroot , '<STR_LIT>' ) ) <EOL> self . _do_test_linkify ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_linkify_stores_values_in_memo ( self ) : <EOL> url = '<STR_LIT>' <EOL> memo = { } <EOL> self . _do_test_linkify ( url , url , memo ) <EOL> self . assertEqual ( url , memo [ url ] ) <EOL> def test_linkify_ignore_many_dots ( self ) : <EOL> url = '<STR_LIT>' <EOL> self . _do_test_not_linkified ( url ) </s>
