import os
import stat

from dvc.main import main
from tests.basic_env import TestDvc


class TestUnprotect(TestDvc):
    def test(self):
        ret = main(["config", "cache.protected", "true"])
        self.assertEqual(ret, 0)

        ret = main(["add", self.FOO])
        self.assertEqual(ret, 0)

        self.assertNotEqual(
            os.stat(self.FOO).st_mode
            & (stat.S_IREAD | stat.S_IRGRP | stat.S_IROTH),
            0,
        )
        self.assertEqual(
            os.stat(self.FOO).st_mode
            & (stat.S_IWRITE | stat.S_IWGRP | stat.S_IWOTH),
            0,
        )

        ret = main(["unprotect", self.FOO])
        self.assertEqual(ret, 0)

        self.assertNotEqual(
            os.stat(self.FOO).st_mode
            & (stat.S_IREAD | stat.S_IRGRP | stat.S_IROTH | stat.S_IWRITE),
            0,
        )
