def test_replaces_regular_file(self): # A regular file is replaced with a symlink. with open(self.hook('pre-receive'), 'w') as f: f.write('garbage') self.assertRaises(OSError, os.readlink, self.hook('pre-receive')) helpers.ensure_hooks(self.repo_dir) self.assertEqual('hook.py', os.readlink(self.hook('pre-receive')))
def requestReceived(self, command, raw_pathname, params): path = compose_path(self.factory.root, raw_pathname) if command == b'git-upload-pack': subcmd = b'upload-pack' elif command == b'git-receive-pack': subcmd = b'receive-pack' else: self.die(b'Unsupported command in request') return cmd = b'git' args = [b'git', subcmd] if params.pop(b'turnip-stateless-rpc', None): args.append(b'--stateless-rpc') if params.pop(b'turnip-advertise-refs', None): args.append(b'--advertise-refs') args.append(path) env = {} if subcmd == b'receive-pack' and self.factory.hookrpc_handler: # This is a write operation, so prepare config, hooks, the hook # RPC server, and the environment variables that link them up. ensure_config(path) self.hookrpc_key = str(uuid.uuid4()) self.factory.hookrpc_handler.registerKey( self.hookrpc_key, raw_pathname, []) ensure_hooks(path) env[b'TURNIP_HOOK_RPC_SOCK'] = self.factory.hookrpc_sock env[b'TURNIP_HOOK_RPC_KEY'] = self.hookrpc_key self.peer = GitProcessProtocol(self) reactor.spawnProcess(self.peer, cmd, args, env=env)
def test_replaces_hook_py(self): # The hooks themselves are symlinks to hook.py, which is always # kept up to date. with open(self.hook('hook.py'), 'w') as f: f.write('nothing to see here') helpers.ensure_hooks(self.repo_dir) with open(self.hook('hook.py'), 'rb') as actual: expected_path = os.path.join( os.path.dirname(turnip.pack.hooks.__file__), 'hook.py') with open(expected_path, 'rb') as expected: self.assertEqual( hashlib.sha256(expected.read()).hexdigest(), hashlib.sha256(actual.read()).hexdigest()) # The hook is executable. self.assertTrue(os.stat(self.hook('hook.py')).st_mode & stat.S_IXUSR)
def test_fixes_symlink(self): # A symlink with a bad path is fixed. os.symlink('foo', self.hook('pre-receive')) self.assertEqual('foo', os.readlink(self.hook('pre-receive'))) helpers.ensure_hooks(self.repo_dir) self.assertEqual('hook.py', os.readlink(self.hook('pre-receive')))
def test_deletes_random(self): # Unknown files are deleted. os.symlink('foo', self.hook('bar')) self.assertIn('bar', os.listdir(self.hooks_dir)) helpers.ensure_hooks(self.repo_dir) self.assertNotIn('bar', os.listdir(self.hooks_dir))