forked from LykosAI/StabilityMatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipInstallArgsTests.cs
More file actions
143 lines (122 loc) · 4.11 KB
/
PipInstallArgsTests.cs
File metadata and controls
143 lines (122 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using StabilityMatrix.Core.Processes;
using StabilityMatrix.Core.Python;
namespace StabilityMatrix.Tests.Core;
[TestClass]
public class PipInstallArgsTests
{
[TestMethod]
public void TestGetTorch()
{
// Arrange
const string version = "==2.1.0";
// Act
var args = new PipInstallArgs().WithTorch(version).ToProcessArgs().ToString();
// Assert
Assert.AreEqual("torch==2.1.0", args);
}
[TestMethod]
public void TestGetTorchWithExtraIndex()
{
// Arrange
const string version = ">=2.0.0";
const string index = "cu118";
// Act
var args = new PipInstallArgs()
.WithTorch(version)
.WithTorchVision()
.WithTorchExtraIndex(index)
.ToProcessArgs()
.ToString();
// Assert
Assert.AreEqual(
"torch>=2.0.0 torchvision --extra-index-url https://download.pytorch.org/whl/cu118",
args
);
}
[TestMethod]
public void TestGetTorchWithMoreStuff()
{
// Act
var args = new PipInstallArgs()
.AddArg("--pre")
.WithTorch("~=2.0.0")
.WithTorchVision()
.WithTorchExtraIndex("nightly/cpu")
.ToString();
// Assert
Assert.AreEqual(
"--pre torch~=2.0.0 torchvision --extra-index-url https://download.pytorch.org/whl/nightly/cpu",
args
);
}
[TestMethod]
public void TestParsedFromRequirementsTxt()
{
// Arrange
const string requirements = """
torch~=2.0.0
torchvision # comment
--extra-index-url https://example.org
""";
// Act
var args = new PipInstallArgs().WithParsedFromRequirementsTxt(requirements);
// Assert
CollectionAssert.AreEqual(
new[] { "torch~=2.0.0", "torchvision", "--extra-index-url https://example.org" },
args.ToProcessArgs().Select(arg => arg.GetQuotedValue()).ToArray()
);
Assert.AreEqual("torch~=2.0.0 torchvision --extra-index-url https://example.org", args.ToString());
}
[TestMethod]
public void TestParsedFromRequirementsTxt_KeepsEnvironmentMarkerRequirementAsSingleArgument()
{
const string requirements = """
onnxruntime-gpu==1.22.0; python_version < "3.11"
""";
var args = new PipInstallArgs().WithParsedFromRequirementsTxt(requirements).ToProcessArgs();
Assert.AreEqual(1, args.Count());
Assert.AreEqual(
"\"onnxruntime-gpu==1.22.0; python_version < \\\"3.11\\\"\"",
args.Single().GetQuotedValue()
);
Assert.AreEqual(
"\"onnxruntime-gpu==1.22.0; python_version < \\\"3.11\\\"\"",
args.ToString()
);
}
[TestMethod]
public void TestWithUserOverrides()
{
// Arrange
var args = new PipInstallArgs()
.AddArg("numpy")
.WithTorch("==1.0.0")
.WithExtraIndex("https://download.pytorch.org/whl/cu121");
var overrides = new List<PipPackageSpecifierOverride>
{
new()
{
Name = "torch",
Constraint = ">=",
Version = "2.0.0",
Action = PipPackageSpecifierOverrideAction.Update
},
new()
{
Name = "--extra-index-url https://download.pytorch.org/whl/nightly/cpu",
Action = PipPackageSpecifierOverrideAction.Update
}
};
// Act
var resultArgs = args.WithUserOverrides(overrides);
// Assert
Assert.AreEqual(
"numpy torch==1.0.0 --extra-index-url https://download.pytorch.org/whl/cu121",
args.ToString()
);
Assert.AreEqual(
"numpy torch>=2.0.0 --extra-index-url https://download.pytorch.org/whl/nightly/cpu",
resultArgs.ToString()
);
}
}