<s> from __future__ import absolute_import <EOL> from __future__ import unicode_literals <EOL> import collections <EOL> import contextlib <EOL> import io <EOL> import os . path <EOL> import sqlite3 <EOL> import subprocess <EOL> import pytest <EOL> import six <EOL> from git_code_debt . create_tables import create_schema <EOL> from git_code_debt . create_tables import populate_metric_ids <EOL> from git_code_debt . repo_parser import Commit <EOL> from git_code_debt . repo_parser import COMMIT_FORMAT <EOL> from git_code_debt . util . subprocess import cmd_output <EOL> from testing . utilities . auto_namedtuple import auto_namedtuple <EOL> from testing . utilities . cwd import cwd <EOL> @ pytest . yield_fixture <EOL> def tempdir_factory ( tmpdir ) : <EOL> class TmpdirFactory ( object ) : <EOL> def __init__ ( self ) : <EOL> self . tmpdir_count = <NUM_LIT:0> <EOL> def get ( self ) : <EOL> path = tmpdir . join ( six . text_type ( self . tmpdir_count ) ) . strpath <EOL> self . tmpdir_count += <NUM_LIT:1> <EOL> os . mkdir ( path ) <EOL> return path <EOL> yield TmpdirFactory ( ) <EOL> class Sandbox ( collections . namedtuple ( '<STR_LIT>' , [ '<STR_LIT>' ] ) ) : <EOL> __slots__ = ( ) <EOL> @ property <EOL> def db_path ( self ) : <EOL> return os . path . join ( self . directory , '<STR_LIT>' ) <EOL> @ contextlib . contextmanager <EOL> def db ( self ) : <EOL> with sqlite3 . connect ( self . db_path ) as db : <EOL> yield db <EOL> @ pytest . yield_fixture <EOL> def sandbox ( tempdir_factory ) : <EOL> ret = Sandbox ( tempdir_factory . get ( ) ) <EOL> with ret . db ( ) as db : <EOL> create_schema ( db ) <EOL> populate_metric_ids ( db , tuple ( ) , False ) <EOL> yield ret <EOL> @ pytest . yield_fixture <EOL> def cloneable ( tempdir_factory ) : <EOL> repo_path = tempdir_factory . get ( ) <EOL> with cwd ( repo_path ) : <EOL> subprocess . check_call ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:.>' ) ) <EOL> subprocess . check_call ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:foo>' , '<STR_LIT>' ) ) <EOL> yield repo_path <EOL> @ pytest . yield_fixture <EOL> def cloneable_with_commits ( cloneable ) : <EOL> commits = [ ] <EOL> def append_commit ( ) : <EOL> output = cmd_output ( '<STR_LIT>' , '<STR_LIT>' , COMMIT_FORMAT ) <EOL> sha , date = output . splitlines ( ) [ : <NUM_LIT:2> ] <EOL> commits . append ( Commit ( sha , int ( date ) ) ) <EOL> def make_commit ( filename , contents ) : <EOL> with io . open ( filename , '<STR_LIT:w>' ) as file_obj : <EOL> file_obj . write ( contents ) <EOL> subprocess . check_call ( ( '<STR_LIT>' , '<STR_LIT>' , filename ) ) <EOL> subprocess . check_call ( ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' . format ( filename ) , <EOL> ) ) <EOL> append_commit ( ) <EOL> with cwd ( cloneable ) : <EOL> append_commit ( ) <EOL> make_commit ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> make_commit ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> make_commit ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> make_commit ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> yield auto_namedtuple ( path = cloneable , commits = commits ) </s>
