예제 #1
0
    def test_covered_and_uncovered_src(self, mock_num_file_lines):

        # Set an expected source file
        coverage_data = CoverageData()
        coverage_data.add_expected_src('/root_dir', 'uncovered.js')

        # Configure the num file lines for the source file
        num_lines = 10
        mock_num_file_lines.return_value = num_lines

        # Provide coverage info for other source files,
        # but not the one we passed to the constructor
        coverage_data.load_from_dict('/root_dir', '', self.TEST_COVERAGE_DICT)

        # Expect that all sources are reported (including the uncovered src)
        self.assertEqual(coverage_data.src_list(), 
                         [u'/root_dir/src1.js',
                          u'/root_dir/subdir/src2.js',
                          u'/root_dir/uncovered.js'])

        # Expect that the source is reported as 0% covered
        self.assertEqual(coverage_data.line_dict_for_src('/root_dir/uncovered.js'),
                         {line_num: False for line_num in range(num_lines)})

        # Expect that total coverage is smaller because
        # of the uncovered file.
        expected_coverage = 6.0 / (8.0 + num_lines)
        self.assertEqual(coverage_data.total_coverage(), expected_coverage)
예제 #2
0
    def test_uncovered_src(self, mock_num_file_lines):

        # Set an expected source file
        coverage_data = CoverageData()
        coverage_data.add_expected_src('/root_dir', 'src1.js')

        # Configure the num file lines for the source file
        num_lines = 10
        mock_num_file_lines.return_value = num_lines

        # Provide no coverage information (did NOT call `load_from_dict()`)

        # Expect that the source is still reported
        self.assertEqual(coverage_data.src_list(), ['/root_dir/src1.js'])
        self.assertEqual(coverage_data.rel_src_path('/root_dir/src1.js'), 'src1.js')

        # Expect that the source is reported as 0% covered
        self.assertEqual(coverage_data.line_dict_for_src('/root_dir/src1.js'),
                         {line_num: False for line_num in range(num_lines)})

        # Expect that total coverage is 0%
        self.assertEqual(coverage_data.total_coverage(), 0.0)
예제 #3
0
class SuitePageServer(ThreadingMixIn, HTTPServer):
    """
    Serve test suite pages and included JavaScript files.
    """

    protocol_version = 'HTTP/1.1'

    # Request response timeout
    timeout = 5

    # Amount of time to wait for clients to POST coverage info
    # back to the server before timing out.
    COVERAGE_TIMEOUT = 2.0

    # Amount of time to wait between checks that the we
    # have all the coverage info
    COVERAGE_WAIT_TIME = 0.1

    # Returns the `CoverageData` instance used by the server
    # to store coverage data received from the test suites.
    # Since `CoverageData` is thread-safe, it is okay for
    # other processes to write to it asynchronously.
    coverage_data = None

    def __init__(self, suite_desc_list, suite_renderer, jscover_path=None, port=0):
        """
        Initialize the server to serve test runner pages
        and dependencies described by `suite_desc_list`
        (list of `SuiteDescription` instances).

        `jscover_path` is the path to the JSCover JAR file.  If not
        specified, no coverage information will be collected.

        Use `suite_renderer` (a `SuiteRenderer` instance) to
        render the test suite pages.
        """

        # Store dependencies
        self.desc_dict = self._suite_dict_from_list(suite_desc_list)
        self.renderer = suite_renderer
        self._jscover_path = jscover_path

        # Create a dict for source instrumenter services
        # (One for each suite description)
        self.src_instr_dict = {}

        address = ('0.0.0.0', port)
        HTTPServer.__init__(self, address, SuitePageRequestHandler)

    def start(self):
        """
        Start serving pages on an open local port.
        """
        server_thread = threading.Thread(target=self.serve_forever)
        server_thread.daemon = True
        server_thread.start()

        # If we're collecting coverage information
        if self._jscover_path is not None:

            # Create an object to store coverage data we receive
            self.coverage_data = CoverageData()

            # Start each SrcInstrumenter instance if we know where JSCover is
            for suite_name, desc in self.desc_dict.iteritems():

                # Inform the coverage data that we expect this source
                # (report it as 0% if no info received).
                for rel_path in desc.src_paths():
                    self.coverage_data.add_expected_src(desc.root_dir(), rel_path)

                # Create an instrumenter serving files
                # in the suite description root directory
                instr = SrcInstrumenter(desc.root_dir(),
                                        tool_path=self._jscover_path)

                # Start the instrumenter service
                instr.start()

                # Associate the instrumenter with its suite description
                self.src_instr_dict[suite_name] = instr

        else:
            self.src_instr_dict = {}

    def stop(self):
        """
        Stop the server and free the port.
        """

        # Stop each instrumenter service that we started
        for instr in self.src_instr_dict.values():
            instr.stop()

        # Stop the page server and free the port
        self.shutdown()
        self.socket.close()

    def suite_url_list(self):
        """
        Return a list of URLs (unicode strings), where each URL
        is a test suite page containing the JS code to run
        the JavaScript tests.
        """
        return [self.root_url() + u'suite/{}'.format(suite_name)
                for suite_name in self.desc_dict.keys()]

    def root_url(self):
        """
        Return the root URL (including host and port) for the server
        as a unicode string.
        """
        host, port = self.server_address
        return u"http://{}:{}/".format(host, port)

    def all_coverage_data(self):
        """
        Returns a `CoverageData` instance containing all coverage data
        received from running the tests.

        Blocks until all suites have reported coverage data.  If it
        times out waiting for all data, raises a `TimeoutException`.

        If we are not collecting coverage, returns None.
        """
        if self.coverage_data is not None:
            self._block_until(self._has_all_coverage)
            return self.coverage_data

        else:
            return None

    def _block_until(self, success_func):
        """
        Block until `success_func` returns True.
        `success_func` should be a lambda with no argument.
        """

        # Remember when we started
        start_time = time.time()

        # Until we are successful
        while not success_func():

            # See if we've timed out
            if time.time() - start_time > self.COVERAGE_TIMEOUT:
                raise TimeoutError()

            # Wait a little bit before checking again
            time.sleep(self.COVERAGE_WAIT_TIME)

    def _has_all_coverage(self):
        """
        Returns True if and only if every suite
        has coverage information.
        """
        # Retrieve the indices of each suite for which coverage
        # information was reported.
        suite_name_list = self.coverage_data.suite_name_list()

        # Check that we have an index for every suite
        # (This is not the most efficient way to do this --
        # if it becomes a bottleneck, we can revisit.)
        return (sorted(suite_name_list) == sorted(self.desc_dict.keys()))

    @classmethod
    def _suite_dict_from_list(cls, suite_desc_list):
        """
        Given a list of `SuiteDescription` instances, construct
        a dictionary mapping suite names to the instances.

        Raises a `DuplicateSuiteNameError` if two suites have
        the same name.
        """
        suite_dict = {
            suite.suite_name(): suite
            for suite in suite_desc_list
        }

        # Check that we haven't repeated keys
        duplicates = cls._duplicates([suite.suite_name() for suite in suite_desc_list])

        if len(duplicates) > 0:
            msg = "Duplicate suite name(s): {}".format(",".join(duplicates))
            raise DuplicateSuiteNameError(msg)

        return suite_dict

    @classmethod
    def _duplicates(cls, name_list):
        """
        Given a list of strings, return a set of duplicates in the list.
        """
        seen = set()
        duplicates = set()

        for name in name_list:

            # Check if we've already seen the name; if so, add it
            # to the list of duplicates
            if name in seen:
                duplicates.add(name)

            # Add the name to the list of names we've already seen
            else:
                seen.add(name)

        return duplicates