891acc3c570f3aa4840882d96f08457dfa8fb394
[scons.git] / test / Perforce / Perforce.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Test fetching source files from Perforce.
29
30 This test requires that a Perforce server be running on the test system
31 on port 1666, as well as that of course a client must be present.
32 """
33
34 import os
35 import socket
36 import string
37
38 import TestSCons
39
40 test = TestSCons.TestSCons()
41
42 p4 = test.where_is('p4')
43 if not p4:
44     test.skip_test("Could not find 'p4'; skipping test(s).\n")
45
46 user = os.environ.get('USER')
47 if not user:
48     user = os.environ.get('USERNAME')
49 if not user:
50     user = os.environ.get('P4USER')
51
52 host = socket.gethostname()
53
54 # clean out everything
55 try:
56     test.run(program=p4, arguments='-p 1666 obliterate -y //testme/...')
57     test.run(program=p4, arguments='-p 1666 depot -d testme')
58 except TestSCons.TestFailed:
59     pass # it's okay if this fails...it will fail if the depot is clear already.
60
61 # Set up a perforce depot for testing.
62 test.write("depotspec","""# A Perforce Depot Specification.
63 Depot:  testme
64
65 Owner:  %s
66
67 Date:   2003/02/19 17:21:41
68
69 Description:
70         A test depot.
71
72 Type:   local
73
74 Address:        subdir
75
76 Map:    testme/...
77 """ % user)
78
79 test.run(program=p4, arguments='-p 1666 depot -i < depotspec')
80
81 # Now set up 2 clients, one to check in some files, and one to
82 # do the building.
83 clientspec = """# A Perforce Client Specification.
84 Client: %s
85
86 Owner:  %s
87
88 Host: %s
89
90 Description:
91         Created by ccrain.
92
93 Root:   %s
94
95 Options:        noallwrite noclobber nocompress unlocked nomodtime normdir
96
97 LineEnd:        local
98
99 View:
100         %s //%s/...
101 """
102
103 clientspec1 = clientspec % ("testclient1", user, host, test.workpath('import'),
104                             "//testme/foo/...", "testclient1")
105 clientspec2 = clientspec % ("testclient2", user, host, test.workpath('work'),
106                             "//testme/...", "testclient2")
107 test.write("testclient1", clientspec1)
108 test.write("testclient2", clientspec2)
109
110 test.subdir('import', ['import', 'sub'], 'work')
111
112 test.run(program=p4, arguments = '-p 1666 client -i < testclient1')
113 test.run(program=p4, arguments = '-p 1666 client -i < testclient2')
114
115 test.write(['import', 'aaa.in'], "import/aaa.in\n")
116 test.write(['import', 'bbb.in'], "import/bbb.in\n")
117 test.write(['import', 'ccc.in'], "import/ccc.in\n")
118
119 test.write(['import', 'sub', 'SConscript'], """
120 Import("env")
121 env.Cat('ddd.out', 'ddd.in')
122 env.Cat('eee.out', 'eee.in')
123 env.Cat('fff.out', 'fff.in')
124 env.Cat('all', ['ddd.out', 'eee.out', 'fff.out'])
125 """)
126
127 test.write(['import', 'sub', 'ddd.in'], "import/sub/ddd.in\n")
128 test.write(['import', 'sub', 'eee.in'], "import/sub/eee.in\n")
129 test.write(['import', 'sub', 'fff.in'], "import/sub/fff.in\n")
130
131 # Perforce uses the PWD environment variable in preference to the actual cwd
132 os.environ["PWD"] = test.workpath('import')
133 paths = map(os.path.normpath, [ 'sub/ddd.in', 'sub/eee.in', 'sub/fff.in', 'sub/SConscript' ])
134 args = '-p 1666 -c testclient1 add -t binary *.in %s' % string.join(paths)
135 test.run(program=p4, chdir='import', arguments=args)
136
137 test.write('changespec', """
138 Change: new
139
140 Client: testclient1
141
142 User:   %s
143
144 Status: new
145
146 Description:
147         A test check in
148
149 Files:
150         //testme/foo/aaa.in     # add
151         //testme/foo/bbb.in     # add
152         //testme/foo/ccc.in     # add
153         //testme/foo/sub/SConscript     # add
154         //testme/foo/sub/ddd.in # add
155         //testme/foo/sub/eee.in # add
156         //testme/foo/sub/fff.in # add
157 """ % user)
158
159 test.run(program=p4, arguments='-p 1666 -c testclient1 submit -i < changespec')
160
161 test.write(['work', 'SConstruct'], """
162 def cat(env, source, target):
163     target = str(target[0])
164     source = map(str, source)
165     f = open(target, "wb")
166     for src in source:
167         f.write(open(src, "rb").read())
168     f.close()
169 env = Environment(BUILDERS={'Cat':Builder(action=cat)},
170                   P4FLAGS='-p 1666 -c testclient2')
171 env.Cat('aaa.out', 'foo/aaa.in')
172 env.Cat('bbb.out', 'foo/bbb.in')
173 env.Cat('ccc.out', 'foo/ccc.in')
174 env.Cat('all', ['aaa.out', 'bbb.out', 'ccc.out'])
175 env.SourceCode('.', env.Perforce())
176 SConscript('foo/sub/SConscript', 'env')
177 """)
178
179 test.subdir(['work', 'foo'])
180 test.write(['work', 'foo', 'bbb.in'], "work/foo/bbb.in\n")
181
182 test.subdir(['work', 'foo', 'sub'])
183 test.write(['work', 'foo', 'sub', 'eee.in'], "work/foo/sub/eee.in\n")
184
185 test.run(chdir = 'work', arguments = '.')
186 test.fail_test(test.read(['work', 'all']) != "import/aaa.in\nwork/foo/bbb.in\nimport/ccc.in\n")
187 test.fail_test(test.read(['work', 'foo', 'sub', 'all']) != "import/sub/ddd.in\nwork/foo/sub/eee.in\nimport/sub/fff.in\n")
188
189 test.pass_test()