<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> from collections import defaultdict <EOL> from contextlib import contextmanager <EOL> from pants . goal . products import MultipleRootedProducts , Products <EOL> from pants . util . contextutil import temporary_dir <EOL> from pants . util . dirutil import safe_open <EOL> from pants_test . base_test import BaseTest <EOL> class ProductsTest ( BaseTest ) : <EOL> def setUp ( self ) : <EOL> super ( ProductsTest , self ) . setUp ( ) <EOL> self . products = Products ( ) <EOL> def test_require ( self ) : <EOL> self . products . require ( '<STR_LIT:foo>' ) <EOL> self . assertTrue ( self . products . isrequired ( '<STR_LIT:foo>' ) ) <EOL> self . assertFalse ( self . products . isrequired ( '<STR_LIT:bar>' ) ) <EOL> self . assertFalse ( self . products . is_required_data ( '<STR_LIT:foo>' ) ) <EOL> self . assertFalse ( self . products . is_required_data ( '<STR_LIT:bar>' ) ) <EOL> def test_get ( self ) : <EOL> foo_product_mapping1 = self . products . get ( '<STR_LIT:foo>' ) <EOL> foo_product_mapping2 = self . products . get ( '<STR_LIT:foo>' ) <EOL> self . assertIsInstance ( foo_product_mapping1 , Products . ProductMapping ) <EOL> self . assertIs ( foo_product_mapping1 , foo_product_mapping2 ) <EOL> def test_get_does_not_require ( self ) : <EOL> self . assertFalse ( self . products . isrequired ( '<STR_LIT:foo>' ) ) <EOL> self . products . get ( '<STR_LIT:foo>' ) <EOL> self . assertFalse ( self . products . isrequired ( '<STR_LIT:foo>' ) ) <EOL> self . products . require ( '<STR_LIT:foo>' ) <EOL> self . assertTrue ( self . products . isrequired ( '<STR_LIT:foo>' ) ) <EOL> def test_require_data ( self ) : <EOL> self . products . require_data ( '<STR_LIT:foo>' ) <EOL> self . assertTrue ( self . products . is_required_data ( '<STR_LIT:foo>' ) ) <EOL> self . assertFalse ( self . products . is_required_data ( '<STR_LIT:bar>' ) ) <EOL> self . assertFalse ( self . products . isrequired ( '<STR_LIT:foo>' ) ) <EOL> self . assertFalse ( self . products . isrequired ( '<STR_LIT:bar>' ) ) <EOL> def test_get_data ( self ) : <EOL> self . assertIsNone ( self . products . get_data ( '<STR_LIT:foo>' ) ) <EOL> data1 = self . products . get_data ( '<STR_LIT:foo>' , dict ) <EOL> data2 = self . products . get_data ( '<STR_LIT:foo>' , dict ) <EOL> self . assertIsInstance ( data1 , dict ) <EOL> self . assertIs ( data1 , data2 ) <EOL> def test_get_data_does_not_require_data ( self ) : <EOL> self . assertFalse ( self . products . is_required_data ( '<STR_LIT:foo>' ) ) <EOL> self . products . get_data ( '<STR_LIT:foo>' ) <EOL> self . assertFalse ( self . products . is_required_data ( '<STR_LIT:foo>' ) ) <EOL> self . products . require_data ( '<STR_LIT:foo>' ) <EOL> self . assertTrue ( self . products . is_required_data ( '<STR_LIT:foo>' ) ) <EOL> def test_empty_products ( self ) : <EOL> foo_product_mapping = self . products . get ( '<STR_LIT:foo>' ) <EOL> self . assertFalse ( foo_product_mapping ) <EOL> @ contextmanager <EOL> def add_products ( self , context_products , product_type , target , * products ) : <EOL> product_mapping = context_products . get ( product_type ) <EOL> with temporary_dir ( ) as outdir : <EOL> def create_product ( product ) : <EOL> with safe_open ( os . path . join ( outdir , product ) , mode = '<STR_LIT:w>' ) as fp : <EOL> fp . write ( product ) <EOL> return product <EOL> product_mapping . add ( target , outdir , map ( create_product , products ) ) <EOL> yield temporary_dir <EOL> def test_non_empty_products ( self ) : <EOL> target = self . make_target ( '<STR_LIT:c>' ) <EOL> with self . add_products ( self . products , '<STR_LIT:foo>' , target , '<STR_LIT>' ) : <EOL> foo_product_mapping = self . products . get ( '<STR_LIT:foo>' ) <EOL> self . assertTrue ( foo_product_mapping ) <EOL> def test_empty_data ( self ) : <EOL> foo_product_mapping = self . products . get_data ( '<STR_LIT:foo>' ) <EOL> self . assertFalse ( foo_product_mapping ) <EOL> @ contextmanager <EOL> def add_data ( self , context_products , data_type , target , * products ) : <EOL> make_products = lambda : defaultdict ( MultipleRootedProducts ) <EOL> data_by_target = context_products . get_data ( data_type , make_products ) <EOL> with temporary_dir ( ) as outdir : <EOL> def create_product ( product ) : <EOL> abspath = os . path . join ( outdir , product ) <EOL> with safe_open ( abspath , mode = '<STR_LIT:w>' ) as fp : <EOL> fp . write ( product ) <EOL> return abspath <EOL> data_by_target [ target ] . add_abs_paths ( outdir , map ( create_product , products ) ) <EOL> yield temporary_dir <EOL> def test_non_empty_data ( self ) : <EOL> target = self . make_target ( '<STR_LIT:c>' ) <EOL> with self . add_data ( self . products , '<STR_LIT:foo>' , target , '<STR_LIT>' ) : <EOL> foo_product_mapping = self . products . get_data ( '<STR_LIT:foo>' ) <EOL> self . assertTrue ( foo_product_mapping ) </s>
