Skip to content

Commit

Permalink
Merge pull request #1 from qiuhaotc/SupportMultiCodeIndexes
Browse files Browse the repository at this point in the history
Initialize the multi code indexes project
  • Loading branch information
qiuhaotc authored Nov 25, 2020
2 parents 6b7b743 + 3494a85 commit 65c9d92
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 88 deletions.
22 changes: 17 additions & 5 deletions .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
name: .NET Core

on: [push]
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.100
- name: Build with dotnet
run: dotnet build src/CodeIndex.sln --configuration Release
dotnet-version: 3.1.101
- name: Install dependencies
run: |
cd src
dotnet restore
- name: Build CodeIndex
run: dotnet build src/CodeIndex.sln --configuration Release --no-restore
- name: Test CodeIndex
run: |
cd src
dotnet test --no-restore --verbosity normal
21 changes: 21 additions & 0 deletions src/CodeIndex.Common/IndexConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;

namespace CodeIndex.Common
{
public class IndexConfig
{
public Guid Pk { get; set; }
public string IndexName { get; set; }
public string MonitorFolder { get; set; }
public IEnumerable<string> IncludedExtensions { get; set; }
public IEnumerable<string> ExcludedExtensions { get; set; }
public int MaxContentHighlightLength { get; set; }
public IEnumerable<string> ExcludedPaths { get; set; }
public int SaveIntervalSeconds { get; set; }
public string OpenIDEUriFormat { get; set; }
public string MonitorFolderRealPath { get; set; }
public DateTime IndexCreatedDate { get; set; }
public DateTime IndexLastUpdatedDate { get; set; }
}
}
10 changes: 10 additions & 0 deletions src/CodeIndex.Common/IndexStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CodeIndex.Common
{
public enum IndexStatus
{
Created,
Initializing,
Monitoring,
Deleting
}
}
15 changes: 15 additions & 0 deletions src/CodeIndex.Common/IndexStatusInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace CodeIndex.Common
{
public class IndexStatusInfo
{
public IndexStatusInfo(IndexStatus indexStatus, IndexConfig indexConfig)
{
indexConfig.RequireNotNull(nameof(indexConfig));
IndexStatus = indexStatus;
IndexConfig = indexConfig;
}

public IndexStatus IndexStatus { get; set; }
public IndexConfig IndexConfig { get; }
}
}
10 changes: 0 additions & 10 deletions src/CodeIndex.Common/SearchType.cs

This file was deleted.

10 changes: 2 additions & 8 deletions src/CodeIndex.Files/FilePathHelper.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
using System.Linq;
using CodeIndex.Common;

namespace CodeIndex.Files
{
public static class FilePathHelper
public static class FilePathHelper
{
public static string[] GetPaths(string[] paths, bool isInLinux)
{
if (isInLinux)
{
return paths?.Select(u => u.ToUpperInvariant().Replace('\\', '/')).ToArray();
}

return paths?.Select(u => u.ToUpperInvariant().Replace('/', '\\')).ToArray();
return isInLinux ? paths?.Select(u => u.ToUpperInvariant().Replace('\\', '/')).ToArray() : paths?.Select(u => u.ToUpperInvariant().Replace('/', '\\')).ToArray();
}
}
}
11 changes: 11 additions & 0 deletions src/CodeIndex.Server/Pages/IndexManagement.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@page "/IndexManagement"

<div class="row">
<div class="col-sm">
<h3>Index Management</h3>
</div>
</div>

@code {

}
3 changes: 3 additions & 0 deletions src/CodeIndex.Server/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<NavLink class="nav-link" href="/TokenTest" Match="NavLinkMatch.All">
<span class="oi oi-project" aria-hidden="true"></span> Token Test
</NavLink>
<NavLink class="nav-link" href="/IndexManagement" Match="NavLinkMatch.All">
<span class="oi oi-bar-chart" aria-hidden="true"></span> Index Management
</NavLink>
<NavLink class="nav-link" href="/Logs" Match="NavLinkMatch.All">
<span class="oi oi-paperclip" aria-hidden="true"></span> Logs
</NavLink>
Expand Down
16 changes: 8 additions & 8 deletions src/CodeIndex.Test/Common/ArgumentValidationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@ public class ArgumentValidationTest
public void TestRequireContainsElement()
{
Assert.Throws<ArgumentException>(() => ArgumentValidation.RequireContainsElement<IEnumerable<string>>(null, "A"));
Assert.Throws<ArgumentException>(() => ArgumentValidation.RequireContainsElement(Array.Empty<string>(), "A"));
Assert.DoesNotThrow(() => ArgumentValidation.RequireContainsElement(new[] { 1, 2, 3 }, "A"));
Assert.Throws<ArgumentException>(() => Array.Empty<string>().RequireContainsElement("A"));
Assert.DoesNotThrow(() => new[] { 1, 2, 3 }.RequireContainsElement("A"));
}

[Test]
public void TestRequireNotNullOrEmpty()
{
Assert.Throws<ArgumentException>(() => ArgumentValidation.RequireNotNullOrEmpty(null, "A"));
Assert.Throws<ArgumentException>(() => ArgumentValidation.RequireNotNullOrEmpty(string.Empty, "A"));
Assert.DoesNotThrow(() => ArgumentValidation.RequireNotNullOrEmpty("ABC", "A"));
Assert.Throws<ArgumentException>(() => string.Empty.RequireNotNullOrEmpty("A"));
Assert.DoesNotThrow(() => "ABC".RequireNotNullOrEmpty("A"));
}

[Test]
public void TestRequireNotNull()
{
Assert.Throws<ArgumentException>(() => ArgumentValidation.RequireNotNull(null, "A"));
Assert.DoesNotThrow(() => ArgumentValidation.RequireNotNull(string.Empty, "A"));
Assert.DoesNotThrow(() => string.Empty.RequireNotNull("A"));
}

[Test]
public void TestRequireRange()
{
Assert.Throws<ArgumentException>(() => ArgumentValidation.RequireRange(123, "A", 100, 1));
Assert.Throws<ArgumentException>(() => ArgumentValidation.RequireRange(0, "A", 100, 1));
Assert.DoesNotThrow(() => ArgumentValidation.RequireRange(12, "A", 100, 1));
Assert.Throws<ArgumentException>(() => 123.RequireRange("A", 100, 1));
Assert.Throws<ArgumentException>(() => 0.RequireRange("A", 100, 1));
Assert.DoesNotThrow(() => 12.RequireRange("A", 100, 1));
}
}
}
4 changes: 2 additions & 2 deletions src/CodeIndex.Test/Common/CodeSourceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public void TestConstructor()
public void TestGetCodeSource()
{
var dateTime = DateTime.UtcNow;
var fileInfo = new FileInfo("C:\\AAA.txt");
var fileInfo = new FileInfo("AAA.txt");
var source = CodeSource.GetCodeSource(fileInfo, "ABCD");
Assert.AreEqual("AAA.txt", source.FileName);
Assert.AreEqual("ABCD", source.Content);
Assert.AreEqual("txt", source.FileExtension);
Assert.AreEqual("C:\\AAA.txt", source.FilePath);
Assert.AreEqual(fileInfo.FullName, source.FilePath);
Assert.AreEqual(fileInfo.LastWriteTimeUtc, source.LastWriteTimeUtc);
Assert.LessOrEqual(dateTime, source.IndexDate);
Assert.AreNotEqual(string.Empty, source.CodePK);
Expand Down
43 changes: 43 additions & 0 deletions src/CodeIndex.Test/Common/IndexConfigTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using CodeIndex.Common;
using NUnit.Framework;
using System;

namespace CodeIndex.Test
{
class IndexConfigTest
{
[Test]
public void TestConstructor()
{
var pk = Guid.NewGuid();
var config = new IndexConfig
{
ExcludedExtensions = new[] { "A" },
ExcludedPaths = new[] { "B" },
IncludedExtensions = new[] { "C" },
IndexCreatedDate = new DateTime(2020, 1, 1),
IndexLastUpdatedDate = new DateTime(2020, 1, 2),
IndexName = "ABC",
MaxContentHighlightLength = 100,
MonitorFolder = "BCA",
MonitorFolderRealPath = "AAA",
OpenIDEUriFormat = "BBB",
Pk = pk,
SaveIntervalSeconds = 10
};

CollectionAssert.AreEquivalent(config.ExcludedExtensions, new[] { "A" });
CollectionAssert.AreEquivalent(config.ExcludedPaths, new[] { "B" });
CollectionAssert.AreEquivalent(config.IncludedExtensions, new[] { "C" });
Assert.AreEqual(new DateTime(2020, 1, 1), config.IndexCreatedDate);
Assert.AreEqual(new DateTime(2020, 1, 2), config.IndexLastUpdatedDate);
Assert.AreEqual("ABC", config.IndexName);
Assert.AreEqual(100, config.MaxContentHighlightLength);
Assert.AreEqual("BCA", config.MonitorFolder);
Assert.AreEqual("AAA", config.MonitorFolderRealPath);
Assert.AreEqual("BBB", config.OpenIDEUriFormat);
Assert.AreEqual(pk, config.Pk);
Assert.AreEqual(10, config.SaveIntervalSeconds);
}
}
}
18 changes: 18 additions & 0 deletions src/CodeIndex.Test/Common/IndexStatusInfoTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using CodeIndex.Common;
using NUnit.Framework;
using System;

namespace CodeIndex.Test
{
class IndexStatusInfoTest
{
[Test]
public void TestConstructor()
{
var statusInfo = new IndexStatusInfo(IndexStatus.Initializing, new IndexConfig());
Assert.IsNotNull(statusInfo.IndexConfig);
Assert.AreEqual(IndexStatus.Initializing, statusInfo.IndexStatus);
Assert.Throws<ArgumentException>(() => _ = new IndexStatusInfo(default, null));
}
}
}
114 changes: 61 additions & 53 deletions src/CodeIndex.Test/Files/FilesWatcherHelperTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using CodeIndex.Files;
using NUnit.Framework;
Expand All @@ -11,72 +12,79 @@ public class FilesWatcherHelperTest : BaseTest
[Test]
public void TestStartWatch()
{
var renameHit = 0;
var changeHit = 0;
var waitMS = 10;
Directory.CreateDirectory(Path.Combine(TempDir, "SubDir"));

using (var watcher = FilesWatcherHelper.StartWatch(TempDir, OnChangedHandler, OnRenameHandler))
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
File.Create(Path.Combine(TempDir, "AAA.cs")).Close();
Thread.Sleep(waitMS);
Assert.AreEqual(1, changeHit);
Assert.AreEqual(0, renameHit);
var renameHit = 0;
var changeHit = 0;
var waitMS = 10;
Directory.CreateDirectory(Path.Combine(TempDir, "SubDir"));

File.AppendAllText(Path.Combine(TempDir, "AAA.cs"), "12345");
Thread.Sleep(waitMS);
Assert.AreEqual(2, changeHit);
Assert.AreEqual(0, renameHit);
using (var watcher = FilesWatcherHelper.StartWatch(TempDir, OnChangedHandler, OnRenameHandler))
{
File.Create(Path.Combine(TempDir, "AAA.cs")).Close();
Thread.Sleep(waitMS);
Assert.AreEqual(1, changeHit);
Assert.AreEqual(0, renameHit);

File.Move(Path.Combine(TempDir, "AAA.cs"), Path.Combine(TempDir, "BBB.cs"));
Thread.Sleep(waitMS);
Assert.AreEqual(2, changeHit);
Assert.AreEqual(1, renameHit);
File.AppendAllText(Path.Combine(TempDir, "AAA.cs"), "12345");
Thread.Sleep(waitMS);
Assert.AreEqual(2, changeHit);
Assert.AreEqual(0, renameHit);

File.Delete(Path.Combine(TempDir, "BBB.cs"));
Thread.Sleep(waitMS);
Assert.AreEqual(3, changeHit);
Assert.AreEqual(1, renameHit);
File.Move(Path.Combine(TempDir, "AAA.cs"), Path.Combine(TempDir, "BBB.cs"));
Thread.Sleep(waitMS);
Assert.AreEqual(2, changeHit);
Assert.AreEqual(1, renameHit);

File.Create(Path.Combine(TempDir, "SubDir", "AAA.cs")).Close();
Thread.Sleep(waitMS);
Assert.AreEqual(4, changeHit);
Assert.AreEqual(1, renameHit);
File.Delete(Path.Combine(TempDir, "BBB.cs"));
Thread.Sleep(waitMS);
Assert.AreEqual(3, changeHit);
Assert.AreEqual(1, renameHit);

File.AppendAllText(Path.Combine(TempDir, "SubDir", "AAA.cs"), "AA BB");
Thread.Sleep(waitMS);
Assert.AreEqual(6, changeHit, "One for folder, one for file");
Assert.AreEqual(1, renameHit);
File.Create(Path.Combine(TempDir, "SubDir", "AAA.cs")).Close();
Thread.Sleep(waitMS);
Assert.AreEqual(4, changeHit);
Assert.AreEqual(1, renameHit);

Directory.Move(Path.Combine(TempDir, "SubDir"), Path.Combine(TempDir, "SubDir2"));
Thread.Sleep(waitMS);
Assert.AreEqual(6, changeHit);
Assert.AreEqual(2, renameHit);
File.AppendAllText(Path.Combine(TempDir, "SubDir", "AAA.cs"), "AA BB");
Thread.Sleep(waitMS);
Assert.AreEqual(6, changeHit, "One for folder, one for file");
Assert.AreEqual(1, renameHit);

Directory.CreateDirectory(Path.Combine(TempDir, "SubDir3"));
Thread.Sleep(waitMS);
Assert.AreEqual(7, changeHit);
Assert.AreEqual(2, renameHit);
Directory.Move(Path.Combine(TempDir, "SubDir"), Path.Combine(TempDir, "SubDir2"));
Thread.Sleep(waitMS);
Assert.AreEqual(6, changeHit);
Assert.AreEqual(2, renameHit);

File.Create(Path.Combine(TempDir, "CCCC")).Close();
Thread.Sleep(waitMS);
Assert.AreEqual(8, changeHit);
Assert.AreEqual(2, renameHit);
Directory.CreateDirectory(Path.Combine(TempDir, "SubDir3"));
Thread.Sleep(waitMS);
Assert.AreEqual(7, changeHit);
Assert.AreEqual(2, renameHit);

File.SetLastAccessTime(Path.Combine(TempDir, "CCCC"), DateTime.Now.AddDays(1));
Thread.Sleep(waitMS);
Assert.AreEqual(8, changeHit, "Do not watch last access time for file");
Assert.AreEqual(2, renameHit);
}
File.Create(Path.Combine(TempDir, "CCCC")).Close();
Thread.Sleep(waitMS);
Assert.AreEqual(8, changeHit);
Assert.AreEqual(2, renameHit);

void OnRenameHandler(object sender, RenamedEventArgs e)
{
renameHit++;
}
File.SetLastAccessTime(Path.Combine(TempDir, "CCCC"), DateTime.Now.AddDays(1));
Thread.Sleep(waitMS);
Assert.AreEqual(8, changeHit, "Do not watch last access time for file");
Assert.AreEqual(2, renameHit);
}

void OnRenameHandler(object sender, RenamedEventArgs e)
{
renameHit++;
}

void OnChangedHandler(object sender, FileSystemEventArgs e)
void OnChangedHandler(object sender, FileSystemEventArgs e)
{
changeHit++;
}
}
else
{
changeHit++;
Assert.Pass();
}
}
}
Expand Down
Loading

0 comments on commit 65c9d92

Please sign in to comment.