• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

samsmithnz / DotNetCensus / 4773330594

pending completion
4773330594

Pull #90

github

GitHub
Merge 6991c8b68 into 0b3e1d200
Pull Request #90: Code gardening

444 of 486 branches covered (91.36%)

Branch coverage included in aggregate %.

75 of 75 new or added lines in 9 files covered. (100.0%)

912 of 959 relevant lines covered (95.1%)

934.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.8
/src/DotNetCensus.Core/Main.cs
1
using ConsoleTables;
2
using DotNetCensus.Core.Models;
3
using DotNetCensus.Core.Projects;
4

5
namespace DotNetCensus.Core;
6

7
public static class Main
8
{
9
    public static string? GetInventoryResultsAsString(string? directory, Repo? repo, string? file)
10
    {
3✔
11
        DateTime startTime = DateTime.Now;
3✔
12
        List<Project> projects = GetProjects(directory, repo);
3✔
13

14
        //If it's inventory output, remove the full path from each project
15
        if (directory != null)
3✔
16
        {
2✔
17
            foreach (Project item in projects)
112✔
18
            {
53✔
19
                if (OperatingSystem.IsWindows())
53!
20
                {
×
21
                    //Fix any Linux path separators to be Windows ones
22
                    item.Path = item.Path.Replace(directory.Replace("/", "\\"), "");
×
23
                }
×
24
                else
25
                {
53✔
26
                    item.Path = item.Path.Replace(directory, "");
53✔
27
                }
53✔
28
            }
53✔
29
        }
2✔
30

31
        if (string.IsNullOrEmpty(file))
3✔
32
        {
2✔
33
            ConsoleTable table = new("Path", "FileName", "FrameworkCode", "FrameworkName", "Family", "Language", "Status");
2✔
34
            foreach (Project item in projects)
118✔
35
            {
56✔
36
                table.AddRow(item.Path, item.FileName, item.FrameworkCode, item.FrameworkName, item.Family, item.Language, item.Status);
56✔
37
            }
56✔
38
            string result = table.ToMinimalString();
2✔
39
            Console.WriteLine(result);
2✔
40
            Console.WriteLine("Time to process: " + TimingHelper.GetTime(DateTime.Now - startTime));
2✔
41
            return result;
2✔
42
        }
43
        else
44
        {
1✔
45
            //Create a CSV file
46
            StreamWriter sw = File.CreateText(file);
1✔
47
            sw.WriteLine("Path,FileName,FrameworkCode,FrameworkName,Family,Language,Status");
1✔
48
            foreach (Project item in projects)
93✔
49
            {
45✔
50
                sw.WriteLine(item.Path + "," +
45✔
51
                    item.FileName + "," +
45✔
52
                    item.FrameworkCode + "," +
45✔
53
                    item.FrameworkName + "," +
45✔
54
                    item.Family + "," +
45✔
55
                    item.Language + "," +
45✔
56
                    item.Status);
45✔
57
            }
45✔
58
            string? result = sw?.ToString();
1!
59
            sw?.Close();
1✔
60

61
            //FileInfo fileInfo = new(_file);
62
            Console.WriteLine($"Exported results to '{file}'");
1✔
63
            Console.WriteLine("Time to process: " + TimingHelper.GetTime(DateTime.Now - startTime));
1✔
64
            return result;
1✔
65
        }
66
    }
3✔
67

68
    /// <summary>
69
    /// Return a string of the framework summary. Can also write to a file
70
    /// </summary>
71
    /// <param name="directory">directory to scan</param>
72
    /// <param name="repo">GitHub repo to scan</param>
73
    /// <param name="includeTotals">include a totals row</param>
74
    /// <param name="file">output string to a file</param>
75
    /// <returns></returns>
76
    public static string? GetFrameworkSummaryAsString(string? directory, Repo? repo, bool includeTotals, string? file)
77
    {
13✔
78
        DateTime startTime = DateTime.Now;
13✔
79
        List<FrameworkSummary> frameworks = GetFrameworkSummary(directory, repo, includeTotals);
13✔
80

81
        if (string.IsNullOrEmpty(file))
13✔
82
        {
12✔
83
            //Create and output the table
84
            ConsoleTable table = new("Framework", "FrameworkFamily", "Count", "Status");
12✔
85
            foreach (FrameworkSummary item in frameworks)
442✔
86
            {
203✔
87
                table.AddRow(item.Framework, item.FrameworkFamily, item.Count, item.Status);
203✔
88
            }
203✔
89
            string result = table.ToMinimalString();
12✔
90
            Console.WriteLine(result);
12✔
91
            Console.WriteLine("Time to process: " + TimingHelper.GetTime(DateTime.Now - startTime));
12✔
92
            return result;
12✔
93
        }
94
        else
95
        {
1✔
96
            //Create a CSV file
97
            StreamWriter sw = File.CreateText(file);
1✔
98
            sw.WriteLine("Framework,FrameworkFamily,Count,Status");
1✔
99
            foreach (FrameworkSummary item in frameworks)
59✔
100
            {
28✔
101
                sw.WriteLine(item.Framework + "," +
28✔
102
                    item.FrameworkFamily + "," +
28✔
103
                    item.Count + "," +
28✔
104
                    item.Status);
28✔
105
            }
28✔
106
            string? result = sw?.ToString();
1!
107
            sw?.Close();
1✔
108

109
            //FileInfo fileInfo = new(_file);
110
            Console.WriteLine($"Exported results to '{file}'");
1✔
111
            Console.WriteLine("Time to process: " + TimingHelper.GetTime(DateTime.Now - startTime));
1✔
112
            return result;
1✔
113
        }
114
    }
13✔
115

116
    /// <summary>
117
    /// Return a list of Framework summary for each framework found 
118
    /// </summary>
119
    /// <param name="directory">directory to scan</param>
120
    /// <param name="repo">GitHub repo to scan</param>
121
    /// <param name="includeTotals">include a totals row</param>
122
    /// <returns></returns>
123
    public static List<FrameworkSummary> GetFrameworkSummary(string? directory, Repo? repo, bool includeTotals)
124
    {
13✔
125
        List<Project> projects = GetProjects(directory, repo);
13✔
126
        List<FrameworkSummary> frameworkSummary = Census.AggregateFrameworks(projects, includeTotals);
13✔
127
        return frameworkSummary;
13✔
128
    }
13✔
129

130

131
    private static List<Project> GetProjects(string? directory, Repo? repo)
132
    {
16✔
133
        List<Project> projects = new();
16✔
134
        List<Project> sortedProjects = new();
16✔
135
        if (!string.IsNullOrEmpty(directory))
16✔
136
        {
8✔
137
            //Run the calculations to get and aggregate the results
138
            projects = DirectoryScanning.SearchDirectory(directory);
8✔
139
        }
8✔
140
        else if (repo != null)
8✔
141
        {
7✔
142
            string? owner = repo.Owner;
7✔
143
            string? repository = repo.Repository;
7✔
144
            string? clientId = repo.User;
7✔
145
            string? clientSecret = repo.Password;
7✔
146
            string? branch = repo.Branch;
7✔
147
            if (string.IsNullOrEmpty(branch))
7✔
148
            {
2✔
149
                branch = "main";
2✔
150
            }
2✔
151
            projects = Task.Run(async () =>
7✔
152
                await RepoScanning.SearchRepo(clientId, clientSecret,
14✔
153
                owner, repository, branch)).Result;
14✔
154
        }
7✔
155
        //Need to sort so that Linux + Windows results are the same
156
        if (projects != null)
16✔
157
        {
16✔
158
            sortedProjects = projects.OrderBy(o => o.Path).ToList();
505✔
159
        }
16✔
160
        return sortedProjects;
16✔
161
    }
16✔
162
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc