import os
import stat
import os.path
import shutil

import unittest
import test_base

from BackupItemSvn import *

class BackupItemSvnTest(unittest.TestCase):
	
	def testDoBackup(self):
		repoName = "svn_test_repo"
		backupBaseDirPath = test_base.tmpDirPath
		repoPath = os.path.join(backupBaseDirPath, repoName)
		
		archiveName = "svn_backup_dir"
		destArchivePath = os.path.join(backupBaseDirPath, archiveName)
		
		# create the repo
		os.system("svnadmin create " + repoPath)
		
		bb = BackupItemSvn(backupBaseDirPath, archiveName)
		bb.setRepoList([repoPath])
		bb.doBackup()
		
		# unfortunately svn makes some files read only whiche mess this up on windows so need to make writable
		
		self._chmodTree(repoPath, 0666, 0666)
		shutil.rmtree(repoPath)
		self._chmodTree(destArchivePath, 0666, 0666)
		shutil.rmtree(destArchivePath)
	
	def _chmodTree(self, path, mode, mask):
		def visit(arg, dirname, names):
			mode, mask = arg
			for name in names:
				fullname = os.path.join(dirname, name)
				if not os.path.islink(fullname):
					new_mode = (os.stat(fullname)[stat.ST_MODE] & ~mask) | mode
					os.chmod(fullname, new_mode)
		os.path.walk(path, visit, (mode, mask))
