aboutsummaryrefslogtreecommitdiff
path: root/glslc/test/option_dash_o.py
blob: cd7f1d1fb659fba87d66b263e152ae22342740ba (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright 2015 The Shaderc Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import expect
import os.path
from glslc_test_framework import inside_glslc_testsuite
from placeholder import FileShader, TempFileName
from builtins import bytes


@inside_glslc_testsuite('OptionDashO')
class TestOptionDashOConcatenatedArg(expect.SuccessfulReturn,
                                     expect.CorrectObjectFilePreamble):
    """Tests that we can concatenate -o and the output filename."""

    shader = FileShader('#version 140\nvoid main() {}', '.vert')
    glslc_args = ['-ofoo', shader]

    def check_output_foo(self, status):
        output_name = os.path.join(status.directory, 'foo')
        return self.verify_object_file_preamble(output_name)


@inside_glslc_testsuite('OptionDashO')
class ManyOutputFilesWithDashO(expect.ErrorMessage):
    """Tests -o and -c with several files generates an error."""

    shader1 = FileShader('', '.vert')
    shader2 = FileShader('', '.frag')
    glslc_args = ['-o', 'foo', '-c', shader1, shader2]
    expected_error = [
        'glslc: error: cannot specify -o when '
        'generating multiple output files\n']


@inside_glslc_testsuite('OptionDashO')
class OutputFileLocation(expect.SuccessfulReturn,
                         expect.CorrectObjectFilePreamble):
    """Tests that the -o flag puts a file in a new location."""

    shader = FileShader('#version 310 es\nvoid main() {}', '.frag')
    glslc_args = [shader, '-o', TempFileName('a.out')]

    def check_output_a_out(self, status):
        output_name = os.path.join(status.directory, 'a.out')
        return self.verify_object_file_preamble(output_name)


@inside_glslc_testsuite('OptionDashO')
class DashOMissingArgumentIsAnError(expect.ErrorMessage):
    """Tests that -o without an argument is an error."""

    glslc_args = ['-o']
    expected_error = ['glslc: error: argument to \'-o\' is missing ' +
                      '(expected 1 value)\n']


@inside_glslc_testsuite('OptionDashO')
class OutputFileBinaryAvoidsCRLFTranslation(expect.ReturnCodeIsZero,
                                            expect.NoOutputOnStderr,
                                            expect.NoGeneratedFiles,
                                            expect.CorrectBinaryLengthAndPreamble):
    """Tests that the -o flag emits a binary file without CR/LF translation.
    """

    # A shader whose compiled output has three bytes that are newlines.
    # If the output stream converts the newlines to CR/LF, then we end up
    # with a file that is 4k + 3 bytes long.  That will be caught by the
    # object file checks.
    SHADER_WITH_THREE_NEWLINES_IN_BINARY = """#version 450
       layout(location = 0) out uint ovar;
       void main() { ovar = 9; }
    """

    shader = FileShader(SHADER_WITH_THREE_NEWLINES_IN_BINARY, '.vert')
    glslc_args = [shader, '-o', '-']

    def check_stdout_binary(self, status):
        binary = bytes(status.stdout)
        newlines = [x for x in binary if x == ord('\n')]
        num_newlines = len(newlines)
        if num_newlines % 4 == 0:
            return False, "Bad test. Need nontrivial number of newlines"
        if num_newlines != 3:
            return False, ("Update this test. Expected 3 newlines in the "
                           "binary, but found {}").format(num_newlines)
        return self.verify_binary_length_and_header(bytes(status.stdout))