From 1f30752375f3cd42db4b1d94d8f88a50abd63f05 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Fri, 18 Sep 2020 13:34:18 +0800 Subject: [PATCH] feat: add test for top fie --- cmd/cloc.go | 20 ++++++++++++++++++++ cmd/cloc_test.go | 27 +++------------------------ cmd/testdata/top_file.txt | 11 +++++++++++ 3 files changed, 34 insertions(+), 24 deletions(-) create mode 100644 cmd/testdata/top_file.txt diff --git a/cmd/cloc.go b/cmd/cloc.go index bcb05aa5..960f86e4 100644 --- a/cmd/cloc.go +++ b/cmd/cloc.go @@ -13,12 +13,14 @@ import ( "log" "os" "path/filepath" + "strconv" "strings" ) type CocaClocConfig struct { ByDirectory bool TopFile bool + TopSizes int } var ( @@ -71,6 +73,23 @@ func processTopFile(dir string) { cloc_app.SortLangeByCode(languageSummaries) + if len(languageSummaries) <= 3 { + for _, summary := range languageSummaries { + fmt.Fprintln(output, "Language: " + summary.Name) + table := cmd_util.NewOutput(output) + table.SetHeader([]string{"Length", "File", "Complexity", "WeightedComplexity"}) + sizes := len(summary.Files) + if sizes >= clocConfig.TopSizes { + sizes = clocConfig.TopSizes + } + + for _, file := range summary.Files[:sizes] { + table.Append([]string{strconv.Itoa(int(file.Code)), file.Language, strconv.Itoa(int(file.Complexity)),strconv.Itoa(int(file.WeightedComplexity)) }) + } + table.Render() + } + } + sortContent, _ := json.MarshalIndent(languageSummaries, "", "\t") cmd_util.WriteToCocaFile("sort_cloc.json", string(sortContent)) } @@ -155,6 +174,7 @@ func addClocConfigs() { flags.BoolVar(&clocConfig.ByDirectory, "by-directory", false, "list directory and out csv") flags.BoolVar(&clocConfig.TopFile, "top-file", false, "list top change file") + flags.IntVar(&clocConfig.TopSizes, "top-size", 30, "top file sizes") flags.Int64Var(&processor.AverageWage, "avg-wage", 56286, "average wage value used for basic COCOMO calculation") flags.BoolVar(&processor.DisableCheckBinary, "binary", false, "disable binary file detection") diff --git a/cmd/cloc_test.go b/cmd/cloc_test.go index e3ae6fe2..35d52251 100644 --- a/cmd/cloc_test.go +++ b/cmd/cloc_test.go @@ -24,13 +24,6 @@ func TestCloc(t *testing.T) { func TestClocByDirectory(t *testing.T) { abs := "../_fixtures/cloc/normal" - analysis := []testcase.CmdTestCase{{ - Name: "analysis", - Cmd: "analysis -p " + abs, - Golden: "", - }} - RunTestCmd(t, analysis) - tests := []testcase.CmdTestCase{{ Name: "cloc", Cmd: "cloc " + abs + " --by-directory --include-ext=java,kt", @@ -42,13 +35,6 @@ func TestClocByDirectory(t *testing.T) { func TestShouldReturnNullWhenIgnoreDir(t *testing.T) { abs := "../_fixtures/cloc/someignore" - analysis := []testcase.CmdTestCase{{ - Name: "analysis", - Cmd: "analysis -p " + abs, - Golden: "", - }} - RunTestCmd(t, analysis) - tests := []testcase.CmdTestCase{{ Name: "cloc", Cmd: "cloc " + abs + " --by-directory", @@ -58,19 +44,12 @@ func TestShouldReturnNullWhenIgnoreDir(t *testing.T) { } func TestShouldByFileSize(t *testing.T) { - abs := "../_fixtures" - - analysis := []testcase.CmdTestCase{{ - Name: "analysis", - Cmd: "analysis -p " + abs, - Golden: "", - }} - RunTestCmd(t, analysis) + abs := "../_fixtures/suggest" tests := []testcase.CmdTestCase{{ Name: "cloc", - Cmd: "cloc " + abs + " --top-file", - Golden: "", + Cmd: "cloc " + abs + " --top-file --top-size=10", + Golden: "testdata/top_file.txt", }} RunTestCmd(t, tests) } \ No newline at end of file diff --git a/cmd/testdata/top_file.txt b/cmd/testdata/top_file.txt new file mode 100644 index 00000000..036ac9a2 --- /dev/null +++ b/cmd/testdata/top_file.txt @@ -0,0 +1,11 @@ +Language: Java +| LENGTH | FILE | COMPLEXITY | WEIGHTEDCOMPLEXITY | +|--------|------|------------|--------------------| +| 40 | Java | 0 | 0 | +| 19 | Java | 0 | 0 | +| 13 | Java | 0 | 0 | +| 4 | Java | 0 | 0 | +Language: JSON +| LENGTH | FILE | COMPLEXITY | WEIGHTEDCOMPLEXITY | +|--------|------|------------|--------------------| +| 810 | JSON | 0 | 0 |