Пример #1
0
        private ResolvePackageAssets InitializeTask(out IEnumerable <PropertyInfo> inputProperties)
        {
            inputProperties = typeof(ResolvePackageAssets)
                              .GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public)
                              .Where(p => !p.IsDefined(typeof(OutputAttribute)) &&
                                     p.Name != nameof(ResolvePackageAssets.DesignTimeBuild))
                              .OrderBy(p => p.Name, StringComparer.Ordinal);

            var requiredProperties = inputProperties
                                     .Where(p => p.IsDefined(typeof(RequiredAttribute)));

            var task = new ResolvePackageAssets();

            // Initialize all required properties as a genuine task invocation would. We do this
            // because HashSettings need not defend against required parameters being null.
            foreach (var property in requiredProperties)
            {
                property.PropertyType.Should().Be(
                    typeof(string),
                    because: $"this test hasn't been updated to handle non-string required task parameters like {property.Name}");

                property.SetValue(task, "_");
            }

            return(task);
        }
Пример #2
0
            public CacheWriter(ResolvePackageAssets task, Stream stream = null)
            {
                _targetFramework = NuGetUtils.ParseFrameworkName(task.TargetFrameworkMoniker);

                _task                      = task;
                _lockFile                  = new LockFileCache(task).GetLockFile(task.ProjectAssetsFile);
                _packageResolver           = NuGetPackageResolver.CreateResolver(_lockFile);
                _compileTimeTarget         = _lockFile.GetTargetAndThrowIfNotFound(_targetFramework, runtime: null);
                _runtimeTarget             = _lockFile.GetTargetAndThrowIfNotFound(_targetFramework, _task.RuntimeIdentifier);
                _stringTable               = new Dictionary <string, int>(InitialStringTableCapacity, StringComparer.Ordinal);
                _metadataStrings           = new List <string>(InitialStringTableCapacity);
                _bufferedMetadata          = new List <int>();
                _platformPackageExclusions = GetPlatformPackageExclusions();

                if (stream == null)
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(task.ProjectAssetsCacheFile));
                    stream  = File.Open(task.ProjectAssetsCacheFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
                    _writer = new BinaryWriter(stream, TextEncoding, leaveOpen: false);
                }
                else
                {
                    _writer = new BinaryWriter(stream, TextEncoding, leaveOpen: true);
                }
            }
Пример #3
0
            public CacheWriter(ResolvePackageAssets task)
            {
                var targetFramework = NuGetUtils.ParseFrameworkName(task.TargetFrameworkMoniker);

                _task              = task;
                _lockFile          = new LockFileCache(task.BuildEngine4).GetLockFile(task.ProjectAssetsFile);
                _packageResolver   = NuGetPackageResolver.CreateResolver(_lockFile, _task.ProjectPath);
                _compileTimeTarget = _lockFile.GetTargetAndThrowIfNotFound(targetFramework, runtime: null);
                _runtimeTarget     = _lockFile.GetTargetAndThrowIfNotFound(targetFramework, _task.RuntimeIdentifier);
                _stringTable       = new Dictionary <string, int>(InitialStringTableCapacity, StringComparer.Ordinal);
                _metadataStrings   = new List <string>(InitialStringTableCapacity);
                _bufferedMetadata  = new List <int>();

                var stream = File.Open(task.ProjectAssetsCacheFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None);

                _writer = new BinaryWriter(stream, TextEncoding, leaveOpen: false);
            }
Пример #4
0
        public void ItHashesAllParameters()
        {
            var inputProperties = typeof(ResolvePackageAssets)
                                  .GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public)
                                  .Where(p => !p.IsDefined(typeof(OutputAttribute)))
                                  .OrderBy(p => p.Name, StringComparer.Ordinal);

            var requiredProperties = inputProperties
                                     .Where(p => p.IsDefined(typeof(RequiredAttribute)));

            var task = new ResolvePackageAssets();

            // Initialize all required properties as a genuine task invocation would. We do this
            // because HashSettings need not defend against required parameters being null.
            foreach (var property in requiredProperties)
            {
                property.PropertyType.Should().Be(
                    typeof(string),
                    because: $"this test hasn't been updated to handle non-string required task parameters like {property.Name}");

                property.SetValue(task, "_");
            }

            byte[] oldHash;
            try
            {
                oldHash = task.HashSettings();
            }
            catch (ArgumentNullException)
            {
                Assert.True(
                    false,
                    "HashSettings is likely not correctly handling null value of one or more optional task parameters");

                throw; // unreachable
            }

            foreach (var property in inputProperties)
            {
                switch (property.PropertyType)
                {
                case var t when t == typeof(bool):
                    property.SetValue(task, true);
                    break;

                case var t when t == typeof(string):
                    property.SetValue(task, property.Name);
                    break;

                default:
                    Assert.True(false, $"{property.Name} is not a bool or string. Update the test code to handle that.");
                    throw null;     // unreachable
                }

                byte[] newHash = task.HashSettings();
                newHash.Should().NotBeEquivalentTo(
                    oldHash,
                    because: $"{property.Name} should be included in hash.");

                oldHash = newHash;
            }
        }