Skip to content
This repository
tree: 6635895bc2
Fetching contributors…

Cannot retrieve contributors at this time

file 66 lines (49 sloc) 1.813 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
# -*- coding: utf8 -*-
import os
import tempfile
from zipfile import BadZipfile, ZipFile

from nose.tools import eq_

from validator.xpi import XPIManager

RESOURCES_PATH = os.path.join(os.path.dirname(__file__), 'resources')


def get_path(fn):
    return os.path.join(RESOURCES_PATH, fn)


def test_open():
    """Test that the manager will open the package."""
    z = XPIManager(get_path('xpi/install_rdf_only.xpi'))
    assert z is not None


def test_get_list():
    """Test that the manager can read the file listing."""
    z = XPIManager(get_path('xpi/install_rdf_only.xpi'))
    assert not z.contents_cache
    assert z.package_contents()
    assert z.contents_cache # Spelling check!
    z.contents_cache = 'foo'
    eq_(z.package_contents(), 'foo')


def test_valid_name():
    "Test that the manager can retrieve the correct file name."
    z = XPIManager(get_path('xpi/install_rdf_only.xpi'))
    contents = z.package_contents()
    assert 'install.rdf' in contents


def test_read_file():
    """Test that a file can be read from the package."""
    z = XPIManager(get_path('xpi/install_rdf_only.xpi'))
    assert z.read('install.rdf') is not None


def test_write_file():
    """Test that a file can be written in UTF-8 to the package."""
    with tempfile.NamedTemporaryFile(delete=False) as t:
        temp_fn = t.name
        try:
            z = XPIManager(temp_fn, mode='w')
            f, d = 'install.rdf', '注目のコレクション'.decode('utf-8')
            z.write(f, d)
            eq_(z.read(f), d.encode('utf-8'))
        finally:
            os.unlink(temp_fn)


def test_missing_file():
    """Tests that the XPI manager correctly reports a missing XPI file."""
    passed = False
    try:
        x = XPIManager('foo.bar')
    except:
        passed = True
    assert passed
Something went wrong with that request. Please try again.