Ejemplo n.º 1
0
        public async Task GitSubmodulesFromDependencies()
        {
            // Initialize remote repository
            using var remote = TemporaryDirectory.Create();
            await ExecuteProcess("git", "init", remote.FullPath);
            await ExecuteProcess("git", "config user.name test", remote.FullPath);
            await ExecuteProcess("git", "config user.email [email protected]", remote.FullPath);

            File.WriteAllText(remote.GetFullPath("test.txt"), "content");
            await ExecuteProcess("git", "add .", remote.FullPath);
            await ExecuteProcess("git", "commit -m commit-message", remote.FullPath);

            // Get remote head
            string head;

            using (var repository = new Repository(remote.FullPath))
            {
                head = repository.Head.Tip.Sha;
                _testOutputHelper.WriteLine("Head: " + head);
            }

            // Initialize current directory
            await ExecuteProcess("git", "init", _directory.FullPath);
            await ExecuteProcess("git", "config user.name test", _directory.FullPath);
            await ExecuteProcess("git", "config user.email [email protected]", _directory.FullPath);

            File.WriteAllText(_directory.GetFullPath("test.txt"), "content");
            await ExecuteProcess("git", "add .", _directory.FullPath);
            await ExecuteProcess("git", "commit -m commit-message", _directory.FullPath);

            // Add submodule
            await ExecuteProcess2("git", new string[] { "submodule", "add", remote.FullPath, "submodule_path" }, _directory.FullPath);

            // List files
            // TODO log file attribute
            var files = Directory.GetFiles(_directory.FullPath, "*", SearchOption.AllDirectories);

            _testOutputHelper.WriteLine("Content of " + _directory.FullPath);
            foreach (var file in files)
            {
                var attr = File.GetAttributes(file);
                _testOutputHelper.WriteLine($"{file} ({attr})");
            }

            // Assert
            var result = await GetDependencies(new GitSubmoduleDependencyScanner());

            AssertContainDependency(result, (DependencyType.GitSubmodule, remote.FullPath, head, 0, 0));

            Assert.All(result, result => Assert.False(result.Location.IsUpdatable));

            async Task ExecuteProcess(string process, string args, string workingDirectory)
            {
                _testOutputHelper.WriteLine($"Executing: '{process}' {args} ({workingDirectory})");
                AssertProcessResult(await ProcessExtensions.RunAsTaskAsync(process, args, workingDirectory));
            }

            async Task ExecuteProcess2(string process, string[] args, string workingDirectory)
            {
                _testOutputHelper.WriteLine($"Executing: '{process}' {string.Join(" ", args)} ({workingDirectory})");
                AssertProcessResult(await ProcessExtensions.RunAsTaskAsync(process, args, workingDirectory));
            }

            void AssertProcessResult(ProcessResult result)
            {
                if (result.ExitCode == 0)
                {
                    _testOutputHelper.WriteLine("git command succeeds\n" + string.Join("\n", result.Output));
                    return;
                }

                Assert.False(true, "git command failed. Logs:\n" + string.Join("\n", result.Output));
            }
        }