Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/shaderc
Path: blob/main/glslc/test/environment.py
1560 views
1
# Copyright 2015 The Shaderc Authors. All rights reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
# http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
15
"""Classes for conveniently specifying a test environment.
16
17
These classes have write() methods that create objects in a test's environment.
18
For instance, File creates a file, and Directory creates a directory with some
19
files or subdirectories in it.
20
21
Example:
22
test.environment = Directory('.', [
23
File('a.vert', 'void main(){}'),
24
Directory('subdir', [
25
File('b', 'b content'),
26
File('c', 'c content')
27
])
28
])
29
30
In general, these classes don't clean up the disk content they create. They
31
were written in a test framework that handles clean-up at a higher level.
32
33
"""
34
35
import os
36
37
class Directory:
38
"""Specifies a directory or a subdirectory."""
39
def __init__(self, name, content):
40
"""content is a list of File or Directory objects."""
41
self.name = name
42
self.content = content
43
44
@staticmethod
45
def create(path):
46
"""Creates a directory path if it doesn't already exist."""
47
try:
48
os.makedirs(path)
49
except OSError: # Handles both pre-existence and a racing creation.
50
if not os.path.isdir(path):
51
raise
52
53
def write(self, parent):
54
"""Creates a self.name directory within parent (which is a string path) and
55
recursively writes the content in it.
56
57
"""
58
full_path = os.path.join(parent, self.name)
59
Directory.create(full_path)
60
for c in self.content:
61
c.write(full_path)
62
63
class File:
64
"""Specifies a file by name and content."""
65
def __init__(self, name, content):
66
self.name = name
67
self.content = content
68
69
def write(self, directory):
70
"""Writes content to directory/name."""
71
with open(os.path.join(directory, self.name), 'w') as f:
72
f.write(self.content)
73
74