Beispiel #1
0
        static void EnsureFilePermissionsOnUnixLikeOS(NPath packageRoot)
        {
            // only necessary on unixes
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return;
            }

            var executablePath = packageRoot.Combine(UncrustifyRelativePathForCurrentPlatform());

            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                var ret = NativeUnix.GetFileMode(executablePath, out var originalPermission);
                if (ret != 0)
                {
                    throw new Exception($"Unable to get execution permission for `{executablePath}`. Error code: {ret}.\nFormatting with this formatter will not work.\nYou may need to set the permission manually by running `chmod u+x {executablePath}` in a console.");
                }

                // set permission for owner to run
                ret = NativeUnix.SetFileMode(executablePath, originalPermission | NativeUnix.UnixFilePermissions.S_IXUSR);
                if (ret != 0)
                {
                    throw new Exception($"Unable to set execution permission on `{executablePath}`. Error code: {ret}.\nFormatting with this formatter will not work.\nYou may set the permission manually by running `chmod u+x {executablePath}` in a console.");
                }
            }
        }
        public void Set_Permission_Works([ValueSource(nameof(PermissionScenarios))] NativeUnix.UnixFilePermissions permission)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return;
            }

            var path = BaseDir.CreateFile("normal.txt");

            Assert.That(NativeUnix.SetFileMode(path, NativeUnix.UnixFilePermissions.None), Is.EqualTo(0));

            NativeUnix.UnixFilePermissions actual;
            Assert.That(NativeUnix.GetFileMode(path, out actual), Is.EqualTo(0));
            Assert.That(actual, Is.EqualTo(NativeUnix.UnixFilePermissions.None));

            Assert.That(NativeUnix.SetFileMode(path, permission), Is.EqualTo(0));

            Assert.That(NativeUnix.GetFileMode(path, out actual), Is.EqualTo(0));
            Assert.That(actual, Is.EqualTo(permission));
        }