Пример #1
0
        static async Task <int> Main(string[] argsArray)
        {
            var args = argsArray.Select(a => a.TrimStart('-').Split('='))
                       .ToDictionary(p => p[0], p => p[1]);

            var root = BazelEnvironment.TryGetWorkspaceRoot();

            if (root != null)
            {
                Directory.SetCurrentDirectory(root);
            }
            else
            {
                root = Directory.GetCurrentDirectory();
            }

            if (args.TryGetValue("tar", out var outputName))
            {
                outputName = outputName.Split(" ")[0];
            }
            else
            {
                outputName = "test.tar.gz";
            }

            var packages = Array.Empty <string>();

            if (args.TryGetValue("packages", out var packagesString))
            {
                packages = packagesString.Split(",");
            }
            using var tarMaker = new TarMaker(outputName, root, packages);
            return(await tarMaker.MakeTar());
        }
Пример #2
0
        public static string CreateTempDirectory()
        {
            var path = Path.Combine(BazelEnvironment.GetTmpDir(), Path.GetRandomFileName());

            Directory.CreateDirectory(path);
            return(path);
        }
Пример #3
0
        public static int Main(string[] args)
        {
            var sdkRoot = Environment.GetEnvironmentVariable("BAZEL_DOTNET_SDKROOT");

            if (sdkRoot == null)
            {
                //"/usr/local/share/dotnet/sdk/5.0.203"
                throw new Exception("BAZEL_DOTNET_SDKROOT is not set, cannot test.");
            }
            sdkRoot = Path.GetFullPath(sdkRoot);
            RulesMSBuild.Tools.Builder.Program.RegisterSdk(sdkRoot);

            var newArgs = new List <string>(args)
            {
                "-nocolor"
            };

            newArgs.Insert(0, typeof(Program).Assembly.Location);

            var testXml = BazelEnvironment.GetTestXmlPath();

            if (!string.IsNullOrEmpty(testXml))
            {
                newArgs.Add("-junit");
                newArgs.Add(testXml);
            }

            int returnCode = Xunit.ConsoleClient.Program.Main(newArgs.ToArray());

            return(returnCode);
        }
Пример #4
0
        public void TestDirectoryBasedCtorArgumentValidation()
        {
            AssertThrows <ArgumentException>(
                () => Runfiles.CreateDirectoryBasedForTesting(null));

            AssertThrows <ArgumentException>(() => Runfiles.CreateDirectoryBasedForTesting(""));

            AssertThrows <ArgumentException>(
                () => Runfiles.CreateDirectoryBasedForTesting("non-existent directory is bad"));

            Runfiles.CreateDirectoryBasedForTesting(BazelEnvironment.GetTmpDir());
        }
Пример #5
0
        public E2eTests(ITestOutputHelper helper)
        {
            _helper = helper;

            _tmp = BazelEnvironment.GetTmpDir(nameof(E2eTests));
            string execRoot = Path.Combine(_tmp, "execroot");

            Directory.CreateDirectory(execRoot);
            Directory.SetCurrentDirectory(execRoot);
            _execRoot = Directory.GetCurrentDirectory(); // in case _execRoot is a symlink (/var => /private/var)
            _tmp      = Path.GetDirectoryName(_execRoot) !;
        }
Пример #6
0
        public int Run()
        {
            var testTmpDir = BazelEnvironment.GetTmpDir();

            Info($"Creating test directory with {testTmpDir}");
            // assumes we're not in a sandbox i.e. tags = ["local"]
            var execRootIndex = testTmpDir.IndexOf("/execroot/", StringComparison.OrdinalIgnoreCase);

            if (execRootIndex < 0)
            {
                throw new Exception($"Bad tmpdir: {testTmpDir}");
            }
            var outputBaseDir  = testTmpDir[..execRootIndex];
Пример #7
0
        public void TestDirectoryBasedRlocation()
        {
            // The DirectoryBased implementation simply joins the runfiles directory and the runfile's path
            // on a "/". DirectoryBased does not perform any normalization, nor does it check that the path
            // exists.
            var dir = Path.Combine(BazelEnvironment.GetTmpDir() !, "mock/runfiles");

            Directory.CreateDirectory(dir).Exists.Should().Be(true);

            var r = Runfiles.CreateDirectoryBasedForTesting(dir);

            // Escaping for "\": once for string and once for regex.
            r.Rlocation("arg").Should().MatchRegex(@".*[/\\]mock[/\\]runfiles[/\\]arg");
        }
Пример #8
0
        static void Main(string[] args)
        {
            var runfiles = Runfiles.Create();

            var root    = BazelEnvironment.GetWorkspaceRoot();
            var tocList = new List <(string, string)>();

            for (var i = 0; i < args.Length; i += 2)
            {
                var label = new Label(args[i]);
                var file  = args[i + 1];
                Info($"Processing: {label.RawValue} => {file}");
                var rlocation = runfiles.Rlocation($"rules_msbuild/{file}");
                var contents  = File.ReadAllText(rlocation);

                var destRel = string.Join('/', "docs", Path.GetFileName(label.Package) + ".md");
                var dest    = Path.GetFullPath(Path.Combine(root, destRel));

                contents = LabelLinkRegex.Replace(contents, (match) =>
                {
                    var labelLink = new Label(match.Groups["label"].Value);
                    string package;
                    if (labelLink.IsRelative)
                    {
                        package = labelLink.Package;
                    }
                    else
                    {
                        package = labelLink.Package;
                    }

                    var labelRel   = string.Join('/', package, labelLink.Name);
                    var targetFile = Path.GetFullPath(Path.Combine(root, labelRel));
                    var rel        = Path.GetRelativePath(Path.GetDirectoryName(dest) !, targetFile).Replace('\\', '/');
                    Info($"{labelLink.RawValue} => {rel}");
                    return($"[{match.Groups["link_text"]}]({rel})");
                });
Пример #9
0
        private static int Restore(RestoreOptions restore)
        {
            var workspace = BazelEnvironment.TryGetWorkspaceRoot();
            var cwd       = Directory.GetCurrentDirectory();

            if (workspace != null && !cwd.StartsWith(workspace))
            {
                Directory.SetCurrentDirectory(workspace);
            }

            var query = $"kind(msbuild_restore,{restore.Package})";

            Console.WriteLine($"bazel query {query} | xargs bazel build");
            var info = new ProcessStartInfo("bazel")
            {
                RedirectStandardOutput = true
            };

            info.ArgumentList.Add("query");
            info.ArgumentList.Add(query);

            var queryProcess = Process.Start(info) !;
            var output       = queryProcess.StandardOutput.ReadToEnd();

            queryProcess.WaitForExit();
            Console.WriteLine(output);
            if (queryProcess.ExitCode != 0)
            {
                return(queryProcess.ExitCode);
            }
            var targets = output.Split(Environment.NewLine);
            var build   = Process.Start("bazel", "build " + string.Join(' ', targets)) !;

            build.WaitForExit();

            return(build.ExitCode);
        }
        public void WorkspaceInit(string workspaceName)
        {
            _testDir = BazelEnvironment.GetTmpDir($"{nameof(WorkspaceInit)}_{workspaceName}");
            var specs = CollectSpecs(workspaceName);

            var templates = new Templates()
            {
                Workspace        = new Template("WORKSPACE", "FAKE_WORKSPACE"),
                RootBuild        = new Template("BUILD.bazel", "FAKE_ROOT_BUILD"),
                BazelProps       = new Template("Bazel.props", "FAKE_BAZEL_PROPS"),
                BazelTargets     = new Template("Bazel.targets", "FAKE_BAZEL_TARGETS"),
                DirectoryProps   = new Template("Directory.Build.props", "FAKE_DIRECTORY_PROPS"),
                DirectoryTargets = new Template("Directory.Build.targets", "FAKE_DIRECTORY_TARGETS"),
                SolutionProps    = new Template("Directory.Solution.props", "FAKE_SOLUTION_PROPS"),
                SolutionTargets  = new Template("Directory.Solution.targets", "FAKE_SOLUTION_TARGETS"),
            };

            foreach (var template in templates.XmlMerge)
            {
                template.Contents = $"<Project>\n    {template.Contents}\n</Project>\n";
            }

            var maker = new WorkspaceMaker(_testDir, workspaceName, templates);

            maker.Init();

            foreach (var spec in specs)
            {
                var info = new FileInfo(Path.Combine(_testDir, spec.Rel));
                info.Exists.Should().BeTrue($"`{spec.Rel}` should have been created.");
                var contents = File.ReadAllLines(info.FullName);
                // var joined = string.Join("\n", contents);
                // foreach (var regex in spec.Regexes)
                // {
                //     regex.IsMatch(joined).Should().BeTrue($"`{regex}` should have matched:\n```\n{joined}\n```");
                // }

                if (spec.Contents.Length > 0)
                {
                    var builder = new StringBuilder().AppendLine(spec.Rel);

                    var expectedIndex = 0;
                    var actualIndex   = 0;

                    var actual   = contents;
                    var expected = spec.Contents;
                    var failed   = false;
                    for (; expectedIndex < expected.Length && actualIndex < actual.Length;)
                    {
                        var e = expected[expectedIndex];
                        var a = actual[actualIndex];
                        if (string.CompareOrdinal(e, a) == 0)
                        {
                            builder.Append("   ");
                            builder.AppendLine(e);
                        }
                        else
                        {
                            failed = true;
                            builder.Append(" - ");
                            builder.AppendLine(e);
                            builder.Append(" + ");
                            builder.AppendLine(a);
                        }

                        expectedIndex++;
                        actualIndex++;
                    }

                    failed.Should().Be(false, builder.ToString());
                }
            }
        }
Пример #11
0
        public static string CreateTempFile()
        {
            var testTmpdir = BazelEnvironment.GetTmpDir();

            return(Path.Combine(testTmpdir, Path.GetRandomFileName()));
        }