Ejemplo n.º 1
0
def test_save_identifier_duplicate():

    # Fail at duplicate add
    identifier_row = get_identifier(None)
    assert not identifier_row.id
    # Duplicate name
    with pytest.raises(IntegrityError):
        assert save_identifier(identifier_row,
                               name=u'identifier_one',
                               obfuscated_name='bbb')

    # Duplicate obfuscated name
    with pytest.raises(IntegrityError):
        assert save_identifier(identifier_row,
                               name=u'identifier_xxx',
                               obfuscated_name='aaa')

    # Duplicate randomly assigned obfuscated name
    identifier_row = get_identifier(None)
    set_random_seed(1)
    assert save_identifier(identifier_row, name=u'identifier_001')
    ident_001_obfuscated_name = get_identifier_by_name(
        u'identifier_001').obfuscated_name
    # Reset random seed to recreate last result
    identifier_row = get_identifier(None)
    set_random_seed(1)
    assert save_identifier(identifier_row, name=u'identifier_002')

    assert get_identifier_by_name(u'identifier_002').obfuscated_name != \
        ident_001_obfuscated_name
Ejemplo n.º 2
0
def test_add_reserveds():
    # Define values to be updated, and their new values
    names_to_update = {
        "variable": "v001",
        "variable_row": "v002",
        "scenario": "s001",
        "decision": "d001",
        "decision_row": "d002"
        }

    for ident_name, obfuscated_name in names_to_update.iteritems():
        identifier_row = get_identifier(None)
        save_identifier(
            identifier_row,
            name=ident_name,
            obfuscated_name=obfuscated_name
            )

    bnf_parser = ObfuscatePythonBNF(get_obfuscated_name)

    # Identifiers after leading reserved attributes should not be obfuscated
    # They should be added to reserved and removed from identifiers
    bnf_parser.attribs.parseString(
        "decision.scenario(variable) = reserved_one.variable "
        "+ view.reserved_one")

    assert bnf_parser.statement.transformString(
        "decision.scenario(variable) = reserved_one.variable") == \
        "d001.s001(variable)=reserved_one.variable"

    assert get_reserved_by_name('variable').primary_package == 'reserved_one'
    assert get_identifier_by_name('variable').obfuscated_name == 'variable'
    with pytest.raises(DoesNotExist):
        assert get_identifier_by_name('view').obfuscated_name != 'view'
Ejemplo n.º 3
0
def test_add_reserveds():
    # Define values to be updated, and their new values
    names_to_update = {
        "variable": "v001",
        "variable_row": "v002",
        "scenario": "s001",
        "decision": "d001",
        "decision_row": "d002"
    }

    for ident_name, obfuscated_name in names_to_update.iteritems():
        identifier_row = get_identifier(None)
        save_identifier(identifier_row,
                        name=ident_name,
                        obfuscated_name=obfuscated_name)

    bnf_parser = ObfuscateKivyBNF(get_obfuscated_name)

    # Identifiers after leading reserved attributes should not be obfuscated
    # They should be added to reserved and removed from identifiers
    bnf_parser.attribs.parseString(
        "decision.scenario(variable) = reserved_one.variable "
        "+ view.reserved_one")

    assert bnf_parser.statement.transformString(
        "decision.scenario(variable) = reserved_one.variable") == \
        "d001.s001(variable)=reserved_one.variable"

    assert get_reserved_by_name('variable').primary_package == 'reserved_one'
    assert get_identifier_by_name('variable').obfuscated_name == 'variable'
    with pytest.raises(DoesNotExist):
        assert get_identifier_by_name('view').obfuscated_name != 'view'
Ejemplo n.º 4
0
def test_get_identifier_by_name():

    # Get good identifier_id
    identifier_row = get_identifier_by_name('identifier_one')
    assert identifier_row.id == 1
    assert identifier_row.name == u'identifier_one'

    # Fail at getting bad identifier_id
    with pytest.raises(DoesNotExist):
        get_identifier_by_name('junk')
Ejemplo n.º 5
0
def test_add_conseq_idents_no_obfuscate():
    bnf_parser = ObfuscateKivyBNF(get_obfuscated_name)

    # Identifiers after leading reserved attributes should not be obfuscated
    # conseq_idents will add identifiers indiscriminately
    bnf_parser.conseq_idents_no_obfuscate.parseString(
        "decision.scenario(variable) = reserved_one.variable")

    assert get_identifier_by_name('decision').obfuscated_name == 'decision'
    assert get_identifier_by_name('scenario').obfuscated_name == 'scenario'
    assert get_identifier_by_name('variable').obfuscated_name == 'variable'
    assert get_identifier_by_name('reserved_one').obfuscated_name == \
        'reserved_one'
Ejemplo n.º 6
0
def test_add_conseq_idents_no_obfuscate():
    bnf_parser = ObfuscatePythonBNF(get_obfuscated_name)

    # Conseq_idents_no_obfuscate will add identifiers without checking
    # whether it is an attribute of a reserved name
    bnf_parser.conseq_idents_no_obfuscate.parseString(
        "decision.scenario(variable) = reserved_one.variable")

    assert get_identifier_by_name('decision').obfuscated_name == 'decision'
    assert get_identifier_by_name('scenario').obfuscated_name == 'scenario'
    assert get_identifier_by_name('variable').obfuscated_name == 'variable'
    assert get_identifier_by_name('reserved_one').obfuscated_name == \
        'reserved_one'
Ejemplo n.º 7
0
def test_add_conseq_idents_no_obfuscate():
    bnf_parser = ObfuscateKivyBNF(get_obfuscated_name)

    # Identifiers after leading reserved attributes should not be obfuscated
    # conseq_idents will add identifiers indiscriminately
    bnf_parser.conseq_idents_no_obfuscate.parseString(
        "decision.scenario(variable) = reserved_one.variable")

    assert get_identifier_by_name('decision').obfuscated_name == 'decision'
    assert get_identifier_by_name('scenario').obfuscated_name == 'scenario'
    assert get_identifier_by_name('variable').obfuscated_name == 'variable'
    assert get_identifier_by_name('reserved_one').obfuscated_name == \
        'reserved_one'
Ejemplo n.º 8
0
def save_reserved(reserved_row, **kwargs):
    """Save a Reserved row."""
    from logic.identifier import get_identifier_by_name, \
        get_identifier_by_obfuscated, save_identifier, get_identifier
    try:
        for name, value in kwargs.iteritems():
            getattr(reserved_row, name)  # Make sure column exists
            setattr(reserved_row, name, value)

    except AttributeError:
        raise

    with obfuscatedb.atomic():
        try:
            reserved_id = save_row(reserved_row, **kwargs)
        except IntegrityError:
            raise

        ####################
        # Update identifiers
        ####################
        if reserved_row.name[0] in [reserved_prefixes.reserved_dir,
                                    reserved_prefixes.non_obfuscated_dir]:
            identifier_name = reserved_row.name[1:]
        elif reserved_row.name[0] in [reserved_prefixes.reserved_file,
                                      reserved_prefixes.non_obfuscated_file]:
            identifier_name = reserved_row.name[1:-3]
        else:
            identifier_name = reserved_row.name

        # Reassign identifier obfuscated name if it exists for another name
        try:
            identifier_row = get_identifier_by_obfuscated(identifier_name)
        except DoesNotExist:
            pass
        else:
            if identifier_row.name != identifier_name:
                identifier_row.obfuscated_name = None
                save_identifier(identifier_row)

        # Unobfuscate name in identifiers
        try:
            identifier_row = get_identifier_by_name(identifier_name)
        except DoesNotExist:
            identifier_row = get_identifier(None)
            save_identifier(
                identifier_row,
                name=identifier_name,
                obfuscated_name=identifier_name)
        else:
            if identifier_row.obfuscated_name != identifier_name:
                save_identifier(
                    identifier_row,
                    name=identifier_name,
                    obfuscated_name=identifier_name)

        return reserved_id
Ejemplo n.º 9
0
def test_save_reserved_reassign():
    from logic.reserved import save_reserved, get_reserved
    from logic.identifier import get_identifier_by_name, save_identifier, \
        get_identifier

    identifier_row = get_identifier(None)
    save_identifier(
        identifier_row,
        name='some_ident',
        obfuscated_name='Reserved'
        )
    assert get_identifier_by_name('some_ident').obfuscated_name == 'Reserved'

    reserved_row = get_reserved(None)
    save_reserved(
        reserved_row,
        name=u'Reserved')

    assert get_identifier_by_name('some_ident').obfuscated_name != 'Reserved'
Ejemplo n.º 10
0
def test_save_identifier_duplicate():

    # Fail at duplicate add
    identifier_row = get_identifier(None)
    assert not identifier_row.id
    # Duplicate name
    with pytest.raises(IntegrityError):
        assert save_identifier(
            identifier_row,
            name=u'identifier_one',
            obfuscated_name='bbb')

    # Duplicate obfuscated name
    with pytest.raises(IntegrityError):
        assert save_identifier(
            identifier_row,
            name=u'identifier_xxx',
            obfuscated_name='aaa')


    # Duplicate randomly assigned obfuscated name
    identifier_row = get_identifier(None)
    set_random_seed(1)
    assert save_identifier(
        identifier_row,
        name=u'identifier_001')
    ident_001_obfuscated_name = get_identifier_by_name(
        u'identifier_001').obfuscated_name
    # Reset random seed to recreate last result
    identifier_row = get_identifier(None)
    set_random_seed(1)
    assert save_identifier(
        identifier_row,
        name=u'identifier_002')

    assert get_identifier_by_name(u'identifier_002').obfuscated_name != \
        ident_001_obfuscated_name
Ejemplo n.º 11
0
def add_identifiers(identifier_list=None, do_obfuscate=True):
    """Add identifier and obfuscated names to Identifiers table.

    Parameters
    ----------
    do_obfuscate : bool
    identifier_list : list
    """
    for identifier_name in identifier_list:
        # Skip identifiers in reserved
        try:
            get_reserved_by_name(identifier_name)
        except DoesNotExist:
            pass
        else:
            continue

        if not do_obfuscate \
                or identifier_name[0:2] == '__' \
                or identifier_name == '__init__.py' \
                or identifier_name.startswith('test_') \
                or identifier_name.endswith('_test.py'):
            obfuscated_name = identifier_name
        else:
            obfuscated_name = ''

        identifier_row = get_identifier(None)
        try:
            save_identifier(identifier_row,
                            name=identifier_name,
                            obfuscated_name=obfuscated_name)
        except IntegrityError as e:
            if 'unique' not in e.message.lower():
                raise
            # If should not be obfuscated, replace obfuscated, o/w pass
            if not do_obfuscate:
                identifier_row = get_identifier_by_name(identifier_name)
                if identifier_row.obfuscated_name != identifier_name:
                    save_identifier(identifier_row,
                                    obfuscated_name=identifier_name)
Ejemplo n.º 12
0
def test_add_reserveds():
    #
    # Add python reserved names
    #
    reserved_list = keyword.kwlist
    add_reserveds('python', reserved_list)

    # Test 'python' in module reserved keywords
    reserved_row = get_reserved_by_name('python')
    assert reserved_row.name == 'python'
    assert reserved_row.primary_package == 'python'

    # Test some python keywords
    reserved_row = get_reserved_by_name('from')
    assert reserved_row.name == 'from'
    assert reserved_row.primary_package == 'python'

    reserved_row = get_reserved_by_name('with')
    assert reserved_row.name == 'with'
    assert reserved_row.primary_package == 'python'

    reserved_row = get_reserved_by_name('class')
    assert reserved_row.name == 'class'
    assert reserved_row.primary_package == 'python'

    #
    # Add kivy reserved names
    #
    reserved_list = [
        'after',
        'App',
        'app',
        'before',
        'bold',
        'BooleanProperty',
        'BoxLayout',
        'Button',
        'canvas',
        'color',
        'disabled',
        'dp',
        'dpi2px',
        'dx',
        'FileChooser',
        'filechooser',
        'FloatLayout',
        'focus',
        'font',
        'GridLayout',
        'halign',
        'height',
        'hint_text',
        'horizontal',
        'id',
        'kivy',
        'Label',
        'Logger',
        'logger',
        'multiline',
        'NumericProperty',
        'ObjectProperty',
        'on_focus',
        'op_press',
        'orientation',
        'padding',
        'platform',
        'properties',
        'px',
        'Rectangle',
        'require',
        'rgba',
        'root',
        'ScreenManager',
        'screenmanager',
        'self',
        'size',
        'size_hint',
        'size_hint_x',
        'size_hint_y',
        'StringProperty',
        'sp',
        'text',
        'text_size',
        'uix',
        'utils',
        'valign',
        'vertical',
    ]
    add_reserveds('kivy', reserved_list)

    # Test some kivy keywords
    reserved_row = get_reserved_by_name('kivy')
    assert reserved_row.name == 'kivy'
    assert reserved_row.primary_package == 'kivy'

    reserved_row = get_reserved_by_name('color')
    assert reserved_row.name == 'color'
    assert reserved_row.primary_package == 'kivy'

    reserved_row = get_reserved_by_name('BoxLayout')
    assert reserved_row.name == 'BoxLayout'
    assert reserved_row.primary_package == 'kivy'

    with pytest.raises(DoesNotExist):
        get_reserved_by_name('junk')

    # Test removing of identifier for new reserved names

    #
    # Add python identifier names
    #
    identifier_list = [
        'variable',
        'variable_row',
    ]
    add_identifiers(identifier_list)

    reserved_list = ['variable']
    add_reserveds('test', reserved_list)

    assert get_identifier_by_name('variable_row').name == 'variable_row'
    assert get_identifier_by_name('variable').obfuscated_name == 'variable'

    #
    # Should not add __init__.py
    #
    reserved_list = ['__init__.py']
    add_reserveds('test', reserved_list, '=')

    with pytest.raises(DoesNotExist):
        get_identifier_by_name('__init__.py')
Ejemplo n.º 13
0
def test_get_dir(tmpdir):
    # Setup reserved folder
    reserved_row = get_reserved(None)
    assert 2 == save_reserved(
        reserved_row,
        name=u'/rsv_dir',
        primary_package='rsv_pkg')

    reserved_row = get_reserved(None)
    assert 3 == save_reserved(
        reserved_row,
        name=u'~rsv_file.py',
        primary_package='rsv_file_pkg')

    reserved_row = get_reserved(None)
    assert 4 == save_reserved(
        reserved_row,
        name=u'#no_obf',
        primary_package='no_obf_pkg')

    reserved_row = get_reserved(None)
    assert 5 == save_reserved(
        reserved_row,
        name=u'=no_obf_file.py')

    # Setup folders and files
    f = io.open(join(str(tmpdir), 'test.py'), 'w')
    f.close()

    f = io.open(join(str(tmpdir), 'rsv_file.py'), 'w')
    f.close()

    included_dir = join(str(tmpdir), 'not_rsv0')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'not_rsv0a.py'), 'w')
    f.close()
    f = io.open(join(included_dir, 'not_rsv0b.py'), 'w')
    f.close()
    included_dir = join(str(tmpdir), 'not_rsv0', 'not_rsv0.1')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'not_rsv0.1.py'), 'w')
    f.close()

    excluded_dir = join(str(tmpdir), 'rsv_dir')
    os.mkdir(excluded_dir)
    excluded_dir = join(str(tmpdir), 'rsv_dir', 'rsv_dir_subdir')
    os.mkdir(excluded_dir)
    f = io.open(join(excluded_dir, 'rsv_subdir_file.py'), 'w')
    f.close()

    included_dir = join(str(tmpdir), 'not_rsv_file')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'not_rsv_file.py'), 'w')
    f.close()

    included_dir = join(str(tmpdir), 'not_rsv0', 'no_obf')
    os.mkdir(included_dir)
    included_dir = join(str(tmpdir), 'not_rsv0', 'no_obf', 'no_obf_1')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'no_obf_1.py'), 'w')
    f.close()

    f = io.open(join(str(tmpdir), 'no_obf_file.py'), 'w')
    f.close()

    # Test reading directory structure
    get_files_gen = file_gen(str(tmpdir))
    num_lines = 0
    for file_name, dir_name, is_reserved_dir, is_reserved_file, \
            do_obfuscate in get_files_gen:
        if file_name == 'test.py':
            assert dir_name == str(tmpdir)
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv0a.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv0b.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv0.1.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0', 'not_rsv0.1')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv_file.py':
            assert dir_name == join(str(tmpdir), 'not_rsv_file')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'rsv_subdir_file.py':
            assert dir_name == join(str(tmpdir), 'rsv_dir', 'rsv_dir_subdir')
            assert is_reserved_dir
            assert is_reserved_file
            assert not do_obfuscate
            assert get_reserved_by_name(
                ''.join(['/', 'rsv_dir_subdir'])).primary_package == 'rsv_pkg'
        if file_name == 'rsv_file.py':
            assert dir_name == str(tmpdir)
            assert not is_reserved_dir
            assert is_reserved_file
            assert not do_obfuscate
        if file_name == 'no_obf_1.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0', 'no_obf', 
                                    'no_obf_1')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert not do_obfuscate
            assert get_reserved_by_name(
                ''.join(['#', 'no_obf_1'])).primary_package == 'no_obf'
        if file_name == 'no_obf_file.py':
            assert dir_name == str(tmpdir)
            assert not is_reserved_dir
            assert not is_reserved_file
            assert not do_obfuscate
        num_lines += 1
    assert num_lines == 9
    assert get_reserved_by_name('~rsv_subdir_file.py').primary_package == \
        'rsv_dir_subdir'
    assert get_identifier_by_name('no_obf_1').name == 'no_obf_1'
Ejemplo n.º 14
0
def test_add_attribs():
    bnf_parser = ObfuscatePythonBNF(get_obfuscated_name)

    # Identifiers after leading reserved attributes should not be obfuscated

    # Attribs should reserve and unobfuscate names in identifiers
    source = "decision.scenario(variable) = reserved_one.variable.another"
    bnf_parser.attribs.parseString(source)
    bnf_parser.conseq_idents.parseString(source)
    assert get_identifier_by_name('decision').obfuscated_name != 'decision'
    assert get_identifier_by_name('scenario').obfuscated_name != 'scenario'
    assert get_identifier_by_name('variable').obfuscated_name == 'variable'
    assert get_identifier_by_name('another').obfuscated_name == 'another'
    assert get_identifier_by_name('reserved_one').obfuscated_name == \
        'reserved_one'

    assert get_reserved_by_name('variable').primary_package == 'reserved_one'
    assert get_reserved_by_name('another').primary_package == 'reserved_one'

    # Attribs should reserve only after reserved name
    source = "decision.scenario.reserved_one.variable2.another2"
    bnf_parser.attribs.parseString(source)
    bnf_parser.conseq_idents.parseString(source)
    assert get_identifier_by_name('decision').obfuscated_name != 'decision'
    assert get_identifier_by_name('scenario').obfuscated_name != 'scenario'
    assert get_identifier_by_name('variable2').obfuscated_name == 'variable2'
    assert get_identifier_by_name('another2').obfuscated_name == 'another2'
    assert get_identifier_by_name('reserved_one').obfuscated_name == \
        'reserved_one'

    # Attribs should reserve only after reserved name, not parameters
    source = "decision.scenario.reserved_one(variable3).another3"
    bnf_parser.attribs.parseString(source)
    bnf_parser.conseq_idents.parseString(source)
    assert get_identifier_by_name('decision').obfuscated_name != 'decision'
    assert get_identifier_by_name('scenario').obfuscated_name != 'scenario'
    assert get_identifier_by_name('variable3').obfuscated_name != 'variable3'
    assert get_identifier_by_name('another3').obfuscated_name != 'another3'
    assert get_identifier_by_name('reserved_one').obfuscated_name == \
        'reserved_one'

    # Attribs should reserve only after reserved name
    source = "decision.scenario.reserved_one.variable4 " \
             "+ variable5.reserved_one.another5"
    bnf_parser.attribs.parseString(source)
    bnf_parser.conseq_idents.parseString(source)
    assert get_identifier_by_name('decision').obfuscated_name != 'decision'
    assert get_identifier_by_name('scenario').obfuscated_name != 'scenario'
    assert get_identifier_by_name('variable4').obfuscated_name == 'variable4'
    assert get_identifier_by_name('variable5').obfuscated_name != 'variable5'
    assert get_identifier_by_name('another5').obfuscated_name == 'another5'
    assert get_identifier_by_name('reserved_one').obfuscated_name == \
        'reserved_one'

    # Attribs should reserve even if they are functions
    source = "decision.scenario.reserved_one.variable6(another6)"
    bnf_parser.attribs.parseString(source)
    bnf_parser.conseq_idents.parseString(source)
    assert get_identifier_by_name('decision').obfuscated_name != 'decision'
    assert get_identifier_by_name('scenario').obfuscated_name != 'scenario'
    assert get_identifier_by_name('variable6').obfuscated_name == 'variable6'
    assert get_identifier_by_name('another6').obfuscated_name != 'another6'
    assert get_identifier_by_name('reserved_one').obfuscated_name == \
           'reserved_one'
Ejemplo n.º 15
0
def test_add_identifiers():
    #
    # Add python identifier names
    #
    identifier_list = [
        'variable',
        'variable_row',
        'search_variables',
        'variable_list',
        'variable_id',
        'select_variable',
        'variable_name',
        'VarValues',
        'scenario',
        'scenario_row',
        'search_scenarios',
        'scenario_list',
        'scenario_id',
        'select_scenario',
        'is_for_scenario_result',
        'is_for_variable_name',
        'decision',
        'decision_row',
        'search_decision',
        'decision_list',
        'decision_id',
        'is_edit_mode'
        ]
    add_identifiers(identifier_list)

    # Test some python keywords
    identifier_row = get_identifier_by_name('scenario')
    assert identifier_row.name == 'scenario'
    assert identifier_row.obfuscated_name != 'scenario'

    identifier_row = get_identifier_by_name('variable_id')
    assert identifier_row.name == 'variable_id'
    assert identifier_row.obfuscated_name != 'variable_id'

    identifier_row = get_identifier_by_name('search_decision')
    assert identifier_row.name == 'search_decision'
    assert identifier_row.obfuscated_name != 'search_decision'

    with pytest.raises(DoesNotExist):
        get_identifier_by_name('junk')

    #
    # Try to add reserved name
    #
    assert get_identifier_by_name('reserved_one').obfuscated_name == \
        'reserved_one'
    identifier_list = [
        'reserved_one'
        ]
    add_identifiers(identifier_list)

    assert get_identifier_by_name('reserved_one').obfuscated_name == \
        'reserved_one'


    #
    # Save a non-obfuscated identifier, should change obfucated_name
    #
    identifier_list = [
        'decision'
        ]
    add_identifiers(identifier_list, do_obfuscate=False)
    assert get_identifier_by_name('decision').obfuscated_name == 'decision'

    #
    # Save a double-underscore name (should not be obfuscated)
    #
    identifier_list = [
        '__decision'
        ]
    add_identifiers(identifier_list)
    assert get_identifier_by_name('__decision').obfuscated_name == '__decision'
Ejemplo n.º 16
0
def test_add_reserveds():
    #
    # Add python reserved names
    #
    reserved_list = keyword.kwlist
    add_reserveds('python', reserved_list)

    # Test 'python' in module reserved keywords
    reserved_row = get_reserved_by_name('python')
    assert reserved_row.name == 'python'
    assert reserved_row.primary_package == 'python'

    # Test some python keywords
    reserved_row = get_reserved_by_name('from')
    assert reserved_row.name == 'from'
    assert reserved_row.primary_package == 'python'

    reserved_row = get_reserved_by_name('with')
    assert reserved_row.name == 'with'
    assert reserved_row.primary_package == 'python'

    reserved_row = get_reserved_by_name('class')
    assert reserved_row.name == 'class'
    assert reserved_row.primary_package == 'python'

    #
    # Add kivy reserved names
    #
    reserved_list = [
        'after', 'App', 'app',
        'before', 'bold', 'BooleanProperty','BoxLayout', 'Button',
        'canvas', 'color',
        'disabled', 'dp', 'dpi2px', 'dx',
        'FileChooser', 'filechooser', 'FloatLayout', 'focus', 'font',
        'GridLayout',
        'halign', 'height', 'hint_text', 'horizontal',
        'id',
        'kivy',
        'Label', 'Logger', 'logger',
        'multiline',
        'NumericProperty',
        'ObjectProperty', 'on_focus', 'op_press', 'orientation',
        'padding', 'platform', 'properties', 'px',
        'Rectangle', 'require', 'rgba', 'root',
        'ScreenManager', 'screenmanager', 'self',
        'size', 'size_hint', 'size_hint_x', 'size_hint_y', 'StringProperty',
        'sp',
        'text', 'text_size',
        'uix', 'utils',
        'valign', 'vertical',
        ]
    add_reserveds('kivy', reserved_list)

    # Test some kivy keywords
    reserved_row = get_reserved_by_name('kivy')
    assert reserved_row.name == 'kivy'
    assert reserved_row.primary_package == 'kivy'

    reserved_row = get_reserved_by_name('color')
    assert reserved_row.name == 'color'
    assert reserved_row.primary_package == 'kivy'

    reserved_row = get_reserved_by_name('BoxLayout')
    assert reserved_row.name == 'BoxLayout'
    assert reserved_row.primary_package == 'kivy'

    with pytest.raises(DoesNotExist):
        get_reserved_by_name('junk')

    # Test removing of identifier for new reserved names

    #
    # Add python identifier names
    #
    identifier_list = [
        'variable',
        'variable_row',
        ]
    add_identifiers(identifier_list)

    reserved_list = ['variable']
    add_reserveds('test', reserved_list)

    assert get_identifier_by_name('variable_row').name == 'variable_row'
    assert get_identifier_by_name('variable').obfuscated_name == 'variable'

    #
    # Should not add __init__.py
    #
    reserved_list = ['__init__.py']
    add_reserveds('test', reserved_list, '=')

    with pytest.raises(DoesNotExist):
        get_identifier_by_name('__init__.py')
Ejemplo n.º 17
0
def test_get_dir(tmpdir):
    # Setup reserved folder
    reserved_row = get_reserved(None)
    assert 2 == save_reserved(reserved_row,
                              name=u'/rsv_dir',
                              primary_package='rsv_pkg')

    reserved_row = get_reserved(None)
    assert 3 == save_reserved(reserved_row,
                              name=u'~rsv_file.py',
                              primary_package='rsv_file_pkg')

    reserved_row = get_reserved(None)
    assert 4 == save_reserved(reserved_row,
                              name=u'#no_obf',
                              primary_package='no_obf_pkg')

    reserved_row = get_reserved(None)
    assert 5 == save_reserved(reserved_row, name=u'=no_obf_file.py')

    # Setup folders and files
    f = io.open(join(str(tmpdir), 'test.py'), 'w')
    f.close()

    f = io.open(join(str(tmpdir), 'rsv_file.py'), 'w')
    f.close()

    included_dir = join(str(tmpdir), 'not_rsv0')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'not_rsv0a.py'), 'w')
    f.close()
    f = io.open(join(included_dir, 'not_rsv0b.py'), 'w')
    f.close()
    included_dir = join(str(tmpdir), 'not_rsv0', 'not_rsv0.1')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'not_rsv0.1.py'), 'w')
    f.close()

    excluded_dir = join(str(tmpdir), 'rsv_dir')
    os.mkdir(excluded_dir)
    excluded_dir = join(str(tmpdir), 'rsv_dir', 'rsv_dir_subdir')
    os.mkdir(excluded_dir)
    f = io.open(join(excluded_dir, 'rsv_subdir_file.py'), 'w')
    f.close()

    included_dir = join(str(tmpdir), 'not_rsv_file')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'not_rsv_file.py'), 'w')
    f.close()

    included_dir = join(str(tmpdir), 'not_rsv0', 'no_obf')
    os.mkdir(included_dir)
    included_dir = join(str(tmpdir), 'not_rsv0', 'no_obf', 'no_obf_1')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'no_obf_1.py'), 'w')
    f.close()

    f = io.open(join(str(tmpdir), 'no_obf_file.py'), 'w')
    f.close()

    # Test reading directory structure
    get_files_gen = file_gen(str(tmpdir))
    num_lines = 0
    for file_name, dir_name, is_reserved_dir, is_reserved_file, \
            do_obfuscate in get_files_gen:
        if file_name == 'test.py':
            assert dir_name == str(tmpdir)
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv0a.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv0b.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv0.1.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0', 'not_rsv0.1')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv_file.py':
            assert dir_name == join(str(tmpdir), 'not_rsv_file')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'rsv_subdir_file.py':
            assert dir_name == join(str(tmpdir), 'rsv_dir', 'rsv_dir_subdir')
            assert is_reserved_dir
            assert is_reserved_file
            assert not do_obfuscate
            assert get_reserved_by_name(''.join(
                ['/', 'rsv_dir_subdir'])).primary_package == 'rsv_pkg'
        if file_name == 'rsv_file.py':
            assert dir_name == str(tmpdir)
            assert not is_reserved_dir
            assert is_reserved_file
            assert not do_obfuscate
        if file_name == 'no_obf_1.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0', 'no_obf',
                                    'no_obf_1')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert not do_obfuscate
            assert get_reserved_by_name(''.join(
                ['#', 'no_obf_1'])).primary_package == 'no_obf'
        if file_name == 'no_obf_file.py':
            assert dir_name == str(tmpdir)
            assert not is_reserved_dir
            assert not is_reserved_file
            assert not do_obfuscate
        num_lines += 1
    assert num_lines == 9
    assert get_reserved_by_name('~rsv_subdir_file.py').primary_package == \
        'rsv_dir_subdir'
    assert get_identifier_by_name('no_obf_1').name == 'no_obf_1'