0
0

More than 5 years have passed since last update.

WindowsでASP.NET Coreをソースからビルドしてテストする

Posted at

ASP.NET Coreをソースからビルドしてユニットテストを実行できるようにするまでの備忘録です。
Windowsでしか試していませんがmacOSの場合はおそらくそんなに困ることもないと思います。
使用したエディタは Visual Studio Code(vscode) です。
2019/6/27 時点での情報です。

参考:Build ASP.NET Core from Source

ソースのダウンロード

git clone --recursive https://github.com/aspnet/AspNetCore

必要ツールのインストール

  • Windows 10, version 1803 or newer
  • At least 10 GB of disk space and a good internet connection (our build scripts download a lot of tools and dependencies)
  • Visual Studio 2019. https://visualstudio.com
  • Git. https://git-scm.org
  • NodeJS. LTS version of 10.14.2 or newer https://nodejs.org
  • Java Development Kit 11 or newer.

Visual Studio2019の必須コンポーネントJDK についてはダウンロードするためのスクリプトが用意されています。
PowerSherll でASP.NET Coreをダウンロードしたフォルダを開き以下のコマンドを実行します。

# VisualStudio 2019の必須コンポーネントインストール
# デフォルトではEnterpriseが使用される
PS> ./eng/scripts/InstallVisualStudio.ps1
# Communityを使用する場合は引数にCommunityをつける
PS> ./eng/scripts/InstallVisualStudio.ps1 Community

# JDKのインストール
PS> ./eng/scripts/InstallJdk.ps1

ビルド

公式では以下のコマンドを使用すればよいと書かれてありますが自分の環境ではうまくいきませんでした。

.\restore.cmd

.NET CoreのMSBuildを使用する、環境変数 VCTargetsPath を設定するとエラーが出なくなりました。

# "VCTargetsPath" に "C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\v140" をセットした状態で下記のコマンドを実行
.\restore.cmd -ForceCoreMsBuild

テスト

vscodeで開くときは必ず PowerShell を使って以下のコマンドを実行します。

# 先頭の"."は必要なので注意
. .\activate.ps1

# グローバルなソリューションは存在しないのでソリューションが存在するフォルダを指定して開く
code .\src\components

この状態でテストのソースを開くと Run TestDebug Test が実行できる状態になっています。
(プロジェクトの解析に時間がかかる可能性があります。問題があるかどうかは問題タブで確認します。)

image.png

おまけ:AspNetCore-Tooling

Razor関連のソースの多くはAspNetCoreのリポジトリではなくAspNetCore-Toolingのリポジトリにあります。

AspNetCore-Toolingのテスト方法は基本的に上記と同じですがいくつか違う部分があります。
以下の点に注意してください。

restore.cmd-ForceCoreMsBuild ではなく -msbuildEngine dotnet を指定する。

.\restore.cmd -msbuildEngine dotnet

デフォルトのままだとテストに.NET Framworkを使用してしまいデバッグできないので強制的に.NET Coreを使用させる。
(適当にpropsファイルを編集します。)

src\Razor\test\Directory.Build.props
<Project>
  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory).., Directory.Build.props))\Directory.Build.props" Condition="'$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Directory.Build.props))'!= ''" />

  <PropertyGroup>
    <ExcludeFromSourceBuild>true</ExcludeFromSourceBuild>
    <DeveloperBuildTestTfms>netcoreapp3.0</DeveloperBuildTestTfms>
    <StandardTestTfms>$(DeveloperBuildTestTfms)</StandardTestTfms>
-   <StandardTestTfms Condition=" '$(DeveloperBuild)' != 'true' ">$(StandardTestTfms)</StandardTestTfms>
-   <StandardTestTfms Condition=" '$(DeveloperBuild)' != 'true' AND '$(OS)' == 'Windows_NT' ">net461;$(StandardTestTfms)</StandardTestTfms>
  </PropertyGroup>
</Project>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0